Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C String array problem

Status
Not open for further replies.

16702

Programmer
Feb 12, 2005
3
CA
I have no idea why this happen to me, plz help.
In a method i have:

char p[255] = "";
char* p2;
char** temp;

for (i = 0 ...){
...
p = "abc" // when i = 0
= "222" // when i = 1
p2 = (char*)p;
p2 = &p2[loc];
temp=p2;
}
however, when I try to print out temp it gives me
abc222
222
instead of
abc
222

thx in advance
 
The code you've posted is not helpful for solving your problem. Either post it complete or don't post ;-)
 
The program is about writing an API on top of NDBM.
Here is the method that I'm talking about.
dbID is an array stores a structure that has info. about a table.
char* key is the key value of the table.
char** outAttrs is where I should store the list of attributes correspond to the key.
since NDBM only support key,value pair, therefore when I get the record from NDBM, I need to parse it.
The format of the record is "020504AABBBBBCCCC", where the numbers are the lengh of each attribute.


int DBE_GetRecord(int tableID, char *key, char **outAttrs){
char *rec;
char* r;
char* t2 = "";
char** temp;
int i, j;
DBM *db;
if (!dbID[tableID].isOpened)
return 2;

db = dbID[tableID].dbm;
datum k = {key, strlen(key)+1};

rec = dbm_fetch(db, k).dptr; // getting the record.
// let rec = "030405abc2222kkkkk";
if (rec == NULL)
return 3; // key not found
for (i = 0; i < dbID[tableID].attrNum; i++){
int loc, len; // location and length of attribute
char p[255] = "";
char* p2;

loc = 2*dbID[tableID].attrNum;

// sums up the length of attribute before attrNumber to find location
for (j = 0; j < 2*i; j+=2){
// convert char to int
loc += 10*(rec[j] - 48);
loc += rec[j+1] - 48;
}
len = 10 * (rec[2*i] - 48);
len = rec[2*i+1] - 48;

strncpy (p,rec,loc+len);
p[len] = NULL;
p2 = (char*)p;
p2 = &p2[loc];
temp=p2;
printf("temp[%d]: %s\n", i, p2);
// this prints out
//abc
//2222
//kkkkk
}

for (i = 0; i < dbID[tableID].attrNum; i++){
printf("in get: %s, len: %d \n", temp,strlen(temp));
//this prints out
//abc2222kkkkk
//2222kkkkk
//kkkkk
}
return 0;
}
 
Wow. I'm amazed temp gives you anything but a segmentation fault. No memory has been assigned to it that I can see.
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Thanx marsd, the code works now. (posted below)

But here comes another problem, the outAttrs doesn't return to caller.
In the caller method:
Code:
char** outAttrs; // have to defined like this
DBE_GEtRecord(2, "aaa", outAttrs);
for (i = 0; i < dbID[id].attrCount; i++)
	printf("%s     ",abc[i]); // seg fault

The callee:
Code:
int DBE_GetRecord(int tableID, char* key, char** outAttrs){
	datum recD;
	char* rec;
	int i, j, loc, len;
	if (dbID[tableID].tableName == NULL)
		return 2;
	outAttrs = (char**)malloc(dbID[tableID].attrCount);
	DBM *db = dbID[tableID].dbm;
	datum k = {key, strlen(key)+1};
	recD = dbm_fetch(db, k);
	if (NULL == (rec = recD.dptr)) return 3; 	// no key found
printf("GetRec:rec = %s\n", rec);	
	for (i = 0; i < dbID[tableID].attrCount; i++){
		loc = 2*dbID[tableID].attrCount;;
		for (j = 0; j < 2*i; j+=2){
			// convert char to int
			loc += 10*(rec[j] - 48);
			loc += rec[j+1] - 48;
		}
		len = 10 * (rec[2*i] - 48);
		len = rec[2*i+1] - 48;

		outAttrs[i] = (char*)malloc(len+1);
		for (j = 0; j < len; j++)
			outAttrs[i][j] = rec[loc+j];
		outAttrs[i][j] = '\0';
		printf("loc: %d    len: %d   \n",loc, len);
		printf("prev[%d] = %s\n", i, outAttrs[i]);
                // prints out correctly
	}
	
	for (i = 0; i < dbID[tableID].attrCount; i++)
		printf("attr[%d] = %s\n", i, outAttrs[i]);
                // prints out correctly too
	return 0;
}

Plz tell me what should i change to make it work.

Thx in advance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top