New to pointers - allocation problem

This is a discussion on New to pointers - allocation problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to use an array of strings as: char *str[MAX] I'm trying to store each a_id in the ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    New to pointers - allocation problem

    Hi,

    I'm trying to use an array of strings as:

    char *str[MAX]

    I'm trying to store each a_id in the str. But after running the follwoing code I find that only the 1st a_id was assigned in all str indices.I've not assigned str to anything!
    I know that its some sort of stale pointer thats creating the problem.How to allocate for str?
    Could you please shed some light upon this?

    Code:
    while(!feof(f))
    		  {
    			fgets (all_id, 100 , f);
    			sscanf (all_id,"%*s %s %s",a_id,d_id);
    			str[i] = a_id;
    Thanks,
    Angkar

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    sscanf (all_id,"%*s %s %s",a_id,d_id);
    Why is there a * there?

    Why it's bad to use feof() to control a loop
    ~Sven
    Last edited by 00Sven; 04-23-2006 at 06:53 PM.
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Whats the solution then?

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    It should probably be
    Code:
     fgets (all_id, 100 , f);
    sscanf (all_id,"%s %s %s",a_id,d_id);
    str[][i] = a_id;
    Identifiers dont have an asterisk.

    When you have an array of strings like this, the first dimension is the actual "string" and the second one is that string's place in the array. Since you indexed the first dimension like that, you overwrote the first character of each string.
    Last edited by whiteflags; 04-23-2006 at 07:01 PM.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    str[][i] = a_id; is not working - a syntax error!
    also * was in sscanf - its used to ignore the string that I dont want to read!

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    I see, well write str*[i] = a_id; istead.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Now if I print within the while loop str is showing all the values!
    But when I try to print outside its losing them!
    Strange!

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Outside the loop, treat it like a normal array.
    Code:
     printf( "%s \n", str[i]);
    where i is whatever string you need.

    You need to dereference str to change it, but not to read.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    There is no difference between *[] and [][] i think!
    And I'm using printf( "%s \n", str[i]); outside the loop!Somhow its value is changin there!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    You need to allocate memory for each row of data
    Code:
    while ( i < MAX && fgets (all_id, 100 , f) != NULL ) {
      if ( sscanf (all_id,"%*s %s %s",a_id,d_id) == 2 ) {
        str[i] = malloc ( strlen(a_id) + 1 );
        strcpy(str[i], a_id);
        i++;
      }
    }
    Assuming that is all your other variables like a_id are arrays and not pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2009, 02:36 PM
  2. Problem with file handling (pointers)
    By hmk in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 11:03 AM
  3. dynamic memory allocation problem
    By firyace in forum C Programming
    Replies: 4
    Last Post: 05-23-2007, 10:57 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21