Thread: A little bit confused...pls help.

  1. #1
    Unregistered
    Guest

    A little bit confused...pls help.

    What is the main difference of the decleration:

    char string[30] and char *string;

    in the program? I mean, why is it that when I retrieved the value of string using char *string in the decleration it displays a null value?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char array[30];

    This allocates a total of 30 'char'acters, including the final one for the null, if you plan on using it with string functions anyway.

    char *string;

    This one is a pointer to a character. This is commonly referred to as a "string". This allocates no space. (Actually, it's not entirely true. It allocates no "string space", but rather, is a pointer, which takes up a tiny bit of space, but it isn't space you can use as your string.) There is no such thing as a (data type of a) 'string' in C. What you do with a "string", is allocate the amount of space you want, or, if it's already allocated some place, you can use this "char *", a 'character pointer', to point at the location of said string.

    I'm tired and don't feel like giving you an entire lesson on pointers right now, but that's the difference between the two.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  2. Bitwise Operators - Setting and Retreiving a Bit
    By Spono in forum C Programming
    Replies: 2
    Last Post: 11-04-2003, 02:09 PM
  3. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM