Thread: Pointer or array

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Pointer or array

    I have a program which receives an input from the user. Should I use an array or a pointer for storing the string.
    Code:
    int main()
    {
    char *prt; //this
    //ptr[100]; //or this?
    printf("Please enter your name: ");
    scanf("%s", prt);
    return 0;
    }
    I would like to know the difference. I have heard someone told me we should use an array, but why?
    lets say i use the pointer, how much memory will be allocated for the string? Also, in what circumstances i should and should not use pointers or arrays?
    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Never, repeat NEVER, use a pointer for storing data.

    A pointer is just a variable that contains an address of something else. Creating a pointer does not create anything for the pointer to point at (the pointee). Writing data through such a pointer yields undefined behaviour .... often, but not always, that means abnormal program termination. It never magically causes memory to be allocated and somehow associated with that pointer.

    If you are going to use a pointer, then YOU have to ensure that pointer contains the address of a suitable area of memory. If you do that, then you can use the pointer as if it was an array.

    For example;
    Code:
    int main()
    {
         char *prt;
         prt = malloc(100);     /*   prt can now be used as if it was an array of 100 characters */
         printf("Please enter your name: ");
         scanf("%s", prt);        /*  This line will break if the user enters more than 99 characters */
         free(ptr);                  /*  Release allocated memory */
         scanf("%s", prt);        /*  This is now invalid */
    return 0;
    }
    Last edited by grumpy; 08-06-2011 at 08:37 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    If you use a pointer, you need to make sure it points somewhere.

    If you don't initialise your pointer, it's like putting your message in an envelope with random characters where the address should be. Sure it might go somewhere (program works), or the postal service may just throw it in the bin as being meaningless (a segfault).

    So these are fairly synonymous:
    Code:
    char array[100];
    scanf("%s",array);
    // or
    scanf("%s",&array[0]);
    
    char *ptr = malloc( 100 * sizeof(*ptr) );
    scanf("%s",ptr);
    
    char array[100];
    char *ptr = array;
    scanf("%s",ptr);
    In each case, the scanf will be able to write up to 100 characters to the memory it has been told about.
    It doesn't know (or care) where exactly that memory is (it only has a pointer to it).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM