Thread: C pointers to arrays - limitation

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    1

    C pointers to arrays - limitation

    I came across the following program under the title 'limitation of array of pointers to strings' in a C book. The book says that

    "the program does not work since the array contains garbage values. And it would be definitely wrong to send these garbage values to scanf() as the addresses where it should store the strings received from the keyboard."

    Code:
    main()
    {
     char *name[4];
     int i;
    
     for(i=0;i<4;i++)
     { 
        printf("Enter name");
        scanf("%s",name[i]);
     }
    }

    I tried executing the same program for integer numbers and it worked. I don't see why this does not work for strings.

    Could anyone please help me with this?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It might work like that by happenstance. The error is that the elements of char *name[4] have 0 bytes of memory allocated to them. The "happenstance" would be that there was unused, noncritical memory where they point to. The more code you add to the program, the less likely this is to remain true -- you will either segfault, or what is worse, overwrite some of your own data and be left unable to figure out why. Or the contents of one of the name pointers would get overwritten, which is less difficult to solve but still no good.

    Each member of name is a char pointer. You have to malloc memory to each of them.
    Code:
    for (i=0;i<4;i++) name[i]=malloc(32);
    Otherwise, if you type "bob", bob requires 3 bytes of storage...that ain't reserved.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    And if you malloc 32 bytes, then you should limit the user from inputing more than 32 bytes into the buffer:
    Code:
    scanf("%31s",name[i]);
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  2. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  5. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM

Tags for this Thread