Thread: Character pointer arrays

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    33

    Character pointer arrays

    Code:
    who knows why the while loop prints correctly while does not proceed to print with the for loop ??...
    char *aqq[30000];
       while(fgets(c,sizeof(c),fq)!=NULL)
       {
         aqq[count]=c; count++;
        printf("%s listed---: ",&aqq[count])
       }
    
       for(i=0;i<=count;i++)
       {
         printf("%s listed---: ",&aqq[i]);
       }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because you are setting every entry in your big array to the same address, that of "c". So when you print all the data, you get whatever the last line was, printed count number of times.

    You would need to create a new string for each line you read, e.g. malloc(strlen(c)+1) and strcpy().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Character pointer arrays
    By axe in forum C Programming
    Replies: 5
    Last Post: 11-15-2007, 04:25 AM
  4. 2D Dynamically allocated pointer arrays
    By Lionmane in forum C Programming
    Replies: 37
    Last Post: 06-11-2005, 10:39 PM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM