char ** problem (ANSI C)
-
I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory:
i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code":fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; }
i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks! -
I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory:
i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code":fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; }
i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!Are you getting (compiler, linker, runtime) errors somewhere? What are you seeing in the debugger?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
Are you getting (compiler, linker, runtime) errors somewhere? What are you seeing in the debugger?
"The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)
-
I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory:
i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code":fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; }
i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!// this is wrong in C or C++ (array is 1 short - will overwrite memory)
cantpalres=i-1;
palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
// this is right (add toss the exta braces)
cantpalres=i;
palres = (char**) malloc (sizeof(char*)*cantpalres);// 1st and 2nd part (simpified)
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
++i; // i++ ok in C but a bad habit in C++
}
cantpalres=i;
// allocate 2 dimensional array of string/charater pointers
palres = (char**) malloc (sizeof(char*)*cantpalres);
if( !palres ) // always check if allocation succeded
return; // or return some error value
fseek (archpalres,0,SEEK_SET);
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
// replace '\n' with '\0'
pNewline = strchr(temp,'\n');
if( pNewline ) // there might not be a newline after last word
*pNewLine = '\0';
len = strlen(temp) + 1; // +1 for '\0'
// allocate array of charaters
palres[i] = (char*) malloc (sizeof(char)*len);
if( palres[i] ) // always check if allocation succeded
strcpy(palres[i],temp);
++i;
}Normaly I would not have given a complete solution, but there was to much wrong too explane it all. INTP
-
I have a text file with reserved c words, one in each line. for example: auto asm break case char const ..(continues) and i want to load all of them in a dynamic char matrix, to reserve memory for all the words in the first dimension, and then for the size of each word, so i declare: char **palres; first of all i go through the file once to know how many reserved words there are, and reserve memory:
i=0; while (!feof(archpalres)) { fgets (temp,MAX_PAL,archpalres); i++; } cantpalres=i-1; palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
then, i go to the beginning of the file, read the length of every reserved word, and i copy it in a temp string. with malloc i reserve memory and try to copy it in the char **palres, but i'm sure there's something i'm doing wrong, here's the code":fseek (archpalres,0,SEEK_SET); j=0; while (!feof(archpalres)) { i=0; c=fgetc(archpalres); while ((c!='\n') && (!feof(archpalres))) { temp[i]=c; i++; c=fgetc(archpalres); } temp[i]=0; *palres = (char*) malloc ((sizeof(char))*i); //is it ok??????????? for (k=0;k<=i;k++) { palres[j][k]=temp[k]; } j++; }
i think i'm not reserving memory in a good way with malloc. I tried checking with the debugger, but i don't understand what it says. It's the first time i use malloc for double (**) pointers. Thanks!Try (*palres) = (char*) malloc ((sizeof(char))*i); parles[j] will also work as you have found out. The problem is in the order of precedence. The brackets will solve this problem. Dave
-
// this is wrong in C or C++ (array is 1 short - will overwrite memory)
cantpalres=i-1;
palres = (char**) malloc ((sizeof(char*))*cantpalres); //is this ok???????????
// this is right (add toss the exta braces)
cantpalres=i;
palres = (char**) malloc (sizeof(char*)*cantpalres);// 1st and 2nd part (simpified)
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
++i; // i++ ok in C but a bad habit in C++
}
cantpalres=i;
// allocate 2 dimensional array of string/charater pointers
palres = (char**) malloc (sizeof(char*)*cantpalres);
if( !palres ) // always check if allocation succeded
return; // or return some error value
fseek (archpalres,0,SEEK_SET);
i=0;
while (!feof(archpalres))
{
if( !fgets (temp,MAX_PAL,archpalres) )
break;
// replace '\n' with '\0'
pNewline = strchr(temp,'\n');
if( pNewline ) // there might not be a newline after last word
*pNewLine = '\0';
len = strlen(temp) + 1; // +1 for '\0'
// allocate array of charaters
palres[i] = (char*) malloc (sizeof(char)*len);
if( palres[i] ) // always check if allocation succeded
strcpy(palres[i],temp);
++i;
}Normaly I would not have given a complete solution, but there was to much wrong too explane it all. INTP