Problem with pointers and string array. C.

Discussion in 'Mac Programming' started by Cheer, Mar 18, 2008.

  1. macrumors newbie

    Joined:
    Mar 18, 2008
    #1
    Hello, could anyone help me with C language a little. I'm trying to write a program with pointers, which would scan some words to the array of strings and would print them on a screen. I'm new to C programming, that's why it's so difficult to deal with such a simple task :confused:
    Code:
    #include <stdio.h> 
    #include <string.h>  
      int main () {    
                   char *  string[3];   
                  int i;  
                  
                   ..............    
              
                 return (0);  }  
    
     
  2. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    To print use printf, in Terminal type man printf, for more information. To read in, use scanf or ideally fgets type man scanf or man fgets for more information.
     
  3. thread starter macrumors newbie

    Joined:
    Mar 18, 2008
    #3
    I was trying to. But there are still some mistakes which I can't find.. :(

    Code:
    
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
    	char* string[3];
    	int j;
    	for(j=0;j<3;j++)
    	{
    	
    	fgets(string[j], sizeof string[j], stdin);
    		
    	}
    
    	for(j=0;j<3;j++)
    	{
    		printf("String number %d: \"%s\"\n", j, string[j]);
    	}
    	return 0;
    } 
     
  4. macrumors member

    Joined:
    Jun 7, 2005
    #4
    If you face a problem like this you need to get hold of the values by debugging, tracing etc.

    F.e. what do you expect the value of
    sizeof string[j]
    to be?
     
  5. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #5
    A pointer only points to a set of characters that were allocated. Since you didn't initialize the pointers to NULL, you really have no idea what they are pointing to.

    You created 3 character pointers, but you haven't allocated any memory for them to point to.

    You can do a couple of things, create a variable (maybe an array of n characters by 3 - a matrix if you will) that you can point the pointer to, or you can allocated memory by calling malloc within your for loop before the fgets call.

    mheidt kind of pointed you in that direction.
     
  6. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    This code, as pilotError mentions, allocates an array of three pointers:
    Code:
    char* string[3] ; 
    
    and it is not an array of strings.

    The array will be 12 bytes long; 3 entries of 4 bytes each; each pointer being 4 bytes long.

    Since you don't initialize them, they will have random data in them. Since you declare these values as pointers, (via char *), your program will expect them to contain pointers.

    However, when you read data via fgets(), you are reading from stdin directly into the storage pointed to by string[j], your pointer field, which, is not pointing to anything meaningful.

    You could / should read into dynamically obtained storage, and put the address of that dynamic memory into your string array entry.

    Todd
     
  7. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #7
    try declaring your "string" variable like this:

    char string[3][100];

    This way, string is a pointer to three buffers of 100 characters each.
    As others pointed out, your current way declares an array of three pointers to characters, but the three pointers don't point to allocated memory until you do something to make sure they are.

    The advantage of my way is that the memory is allocated for you (on the stack). The disadvantage is that the strings are fixed to some constant size (100, in this case) at compile time.
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    And... and.... another point!

    It would behoove you to use a meaningful variable name for your pointer array. "string[3]" is not a good name for the purpose you are using it for. "string_pointer[3]" or some such name would be better and be less confusing to yourself and the people who read your code.

    Todd
     
  9. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #9
    I see your error. You must think the "char *foobar[3]" is an array of three strings. No it is an array of three pointers. You need to alocate space for the actuall strings. Use eithe "srtdup", malloc, or calloc to make the space. See man pages.
     
  10. thread starter macrumors newbie

    Joined:
    Mar 18, 2008
    #10
    Thank's a lot, i did it. I just have never realized that array *string[3] is an array of pointers, and that was my biggest mistake. Thanks a lot ;)
     

Share This Page