zizi21
asked on
urgent - pointers to pointers
hi,
could anyone pls help?
i dont know what is it.
char **str;
str= calloc(5,sizeof(char*));
so that i could have like this
str[0] point to hello (token from file)
str[1] point to world
for testing purpose i did,
printf("Str 0 is %s",str[0]); and i got world instead of hello..
but when i try to access the str[0] at the end of the program, i get junk..could anyone pls help
could anyone pls help?
i dont know what is it.
char **str;
str= calloc(5,sizeof(char*));
so that i could have like this
str[0] point to hello (token from file)
str[1] point to world
for testing purpose i did,
printf("Str 0 is %s",str[0]); and i got world instead of hello..
but when i try to access the str[0] at the end of the program, i get junk..could anyone pls help
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
>> *str = calloc(5,sizeof(char*));
You shouldn't dereference an uninitialized pointer.
>> strcpy(str[i],"test text\n");
You only allocated space for 10 characters. That's not enough for the terminating '\0'.
You shouldn't dereference an uninitialized pointer.
>> strcpy(str[i],"test text\n");
You only allocated space for 10 characters. That's not enough for the terminating '\0'.
>>char **str;
>>i dont know what is it.
Concentrating on this bit of your question ... maybe this would help [added as points.txt - so rename it]?
#include<stdio.h>
int main(void)
{
char c = 'z';
char * cp = &c;
char ** cpp = &cp;
puts("\tConsider the following\n");
puts("char c = 'z';");
puts("char * cp = &c;");
puts("char ** cpp = &cp;");
/* outputs z z z */
puts("\nprintf(\"%c %c %c\", c, *cp, **cpp);");
printf("\n%c %c %c\n\n", c, *cp, **cpp);
printf("c is located in memory at: %p\n\n", &c);
printf("cp is pointing to c, and that means it contains the same value: %p\n\n", cp);
printf("cp is itself located at memory address: %p\n\n", &cp);
printf("and, as cpp is pointing to cp, it contains that same address: %p\n\n", cpp);
printf("so, if we dereference, cpp we'll get: %p\n\n", *cpp);
printf("and, if we dereference that, we'll get c itself again: %c\n\n", **cpp);
return 0;
}
points.txt
>>i dont know what is it.
Concentrating on this bit of your question ... maybe this would help [added as points.txt - so rename it]?
#include<stdio.h>
int main(void)
{
char c = 'z';
char * cp = &c;
char ** cpp = &cp;
puts("\tConsider the following\n");
puts("char c = 'z';");
puts("char * cp = &c;");
puts("char ** cpp = &cp;");
/* outputs z z z */
puts("\nprintf(\"%c %c %c\", c, *cp, **cpp);");
printf("\n%c %c %c\n\n", c, *cp, **cpp);
printf("c is located in memory at: %p\n\n", &c);
printf("cp is pointing to c, and that means it contains the same value: %p\n\n", cp);
printf("cp is itself located at memory address: %p\n\n", &cp);
printf("and, as cpp is pointing to cp, it contains that same address: %p\n\n", cpp);
printf("so, if we dereference, cpp we'll get: %p\n\n", *cpp);
printf("and, if we dereference that, we'll get c itself again: %c\n\n", **cpp);
return 0;
}
points.txt
ASKER
trying oout now..thanks
ASKER
would it matter if the string literal is stored in the token...
while(fgets(maxline,80,fp) !=NULL)
{
maxline[strlen(maxline)]=' \0';
token = strtok(line, limit);
while(token!=NULL)
{
str=token;
token=strtok(NULL,limit);
}
table[i]=token;
...
,...
while(fgets(maxline,80,fp)
{
maxline[strlen(maxline)]='
token = strtok(line, limit);
while(token!=NULL)
{
str=token;
token=strtok(NULL,limit);
}
table[i]=token;
...
,...
while(fgets(maxline,80,fp)!=NULL)
{
maxline[strlen(maxline)]='\0'; // <--- this is unnecessary. fgets already adds the '\0'
token = strtok(line, limit); // <--- didn't you mean to use maxline instead of line ?
while(token!=NULL)
{
str=token; // <--- you're not using str ...
token=strtok(NULL,limit);
}
table[i]=token; // <--- token is NULL here. Is that what you want ?
ASKER
sorry, this is the one..i was typing too fast...
while(fgets(line,80,fp)!=N ULL)
{
token = strtok(line, delimiter);
count=0;
while(token != NULL)
{
count++;
if(count == 2)
str = token;
token = strtok(NULL, delimiter);
}
table[i] =calloc(strlen(str)+1,size of(char));
strcpy(table[i],str);
i++;
}
while(fgets(line,80,fp)!=N
{
token = strtok(line, delimiter);
count=0;
while(token != NULL)
{
count++;
if(count == 2)
str = token;
token = strtok(NULL, delimiter);
}
table[i] =calloc(strlen(str)+1,size
strcpy(table[i],str);
i++;
}
ASKER
i think, i do understand now...
since the str would be holding the value of token, when i change the token and pass it to str to keep the value, it would be holding the latest value...so, the pointer would be pointing to th elatest value...
since the str would be holding the value of token, when i change the token and pass it to str to keep the value, it would be holding the latest value...so, the pointer would be pointing to th elatest value...
I coded this: (in Borland 5.02)
run it! it create 2d array and free allocated memories.
regards
Open in new window