#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Post String array in C programming


    I am new to C and getting lot of small problems.This time i tried to get some user inputs to a char array and print them on console.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    typedef char * string;
     
    int main(void)
    {
      string strs[3];
       int i,j;
      
    	for(j=0; j<3; j++){
    		scanf("%s", &strs[j]);
    	}
       
        for(i=0;i<sizeof(strs)/sizeof(string);++i)
            printf("%s",strs[i]);
       
     
      return 0;
    }
    problem i get is,when program runs, after giving input strings to the program,a window appears showing program needs to close.
    But same program worked fine with integers(using an int array).
    Can someone help me solving this problem??
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    The problem is, you don't have any storage allocated.

    Start with something simple, like
    char strs[3][100];

    before you start obscuring things with a pointer typedef.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by salem
    The problem is, you don't have any storage allocated.

    Start with something simple, like
    char strs[3][100];

    before you start obscuring things with a pointer typedef.
    Thanks! :thumbs:
    It works..But can u explain why should i do so,,I ask this because it worked before with integers without allocating storage.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The int array contained memory assigned to hold the int values. The char* array contains memory assigned to hold pointer values, AKA "addresses". Pointers need to point to something. The pointers in that array contained garbage addresses, just random locations because of what just happened to have been stored in those locations before your program ran. They could point to anywhere, most likely outside the range of memory addresses that your program was given by the operating system. Trying to access locations outside your allotted memory is punishable by the death of your program: it gets terminated with extreme prejudice with a SEGFAULT or ACCESS error. This error you committed is also called "using an uninitialized pointer".

    One solution is to declare a 2D array of char. Another is to malloc or calloc space for those strings and assign the resultant pointer values in the char* array, thus initializing them.

    A pointer must always be initialized to point to somewhere safe.

IMN logo majestic logo threadwatch logo seochat tools logo