Link to home
Start Free TrialLog in
Avatar of zizi21
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
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello
I coded this: (in Borland 5.02)
run it! it create 2d array and free allocated memories.
regards
#include <windows.h>
#include <stdio.h>
int main()
{
int i;
char **str;
//allocate 2d array in heap
*str = calloc(5,sizeof(char*));
for(i=0;i<5;i++)
{
str[i] = (char*)(calloc(10,sizeof(char)));
strcpy(str[i],"test text\n");
printf("%s",str[i]);
}
//free heap
for(i=0;i<5;i++)
free(str[i]);
free(str);
system("pause");
}

Open in new window

>> *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'.
>>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
Avatar of zizi21
zizi21

ASKER

trying oout now..thanks
Avatar of zizi21

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)!=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 ?

Open in new window

Avatar of zizi21

ASKER

sorry, this is the one..i was typing too fast...


while(fgets(line,80,fp)!=NULL)
      {  
            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,sizeof(char));
            
            strcpy(table[i],str);
            
            i++;
      }
Avatar of zizi21

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...