Thread: A question between Array and Pointer

  1. #1
    Unregistered
    Guest

    A question between Array and Pointer

    Program 1:
    #include <iostream.h>

    int main()
    {
    char *nz;
    nz="hello";

    cout << nz << "\n";
    return 0;
    }

    Program 2:
    #include <iostream.h>

    int main()
    {
    char nz[6];
    nz="hello";

    cout << nz << "\n";
    return 0;
    }

    Isnt that,they are the same?howcome the first one run without any error,but the second one got an error?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    Re: A question between Array and Pointer

    Originally posted by Unregistered
    Program 1:
    #include <iostream.h>

    int main()
    {
    char *nz;
    nz="hello";

    cout << nz << "\n";
    return 0;
    }

    Program 2:
    #include <iostream.h>

    int main()
    {
    char nz[6];
    nz="hello";

    cout << nz << "\n";
    return 0;
    }

    Isnt that,they are the same?howcome the first one run without any error,but the second one got an error?
    With the second you are trying to assign a r-value to an l-value. To do what you want to do you have to use the function strcpy( );

    strcpy( nz, "Hello" );

  3. #3
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    In the first program, nz is a string initialized as pointer. It's legal to assign "hello" (nameless) string to nz. Therefore your first program works perfectly.

    However, in the second program, nz is a C string. And in order to assign "hello" to nz string, you must put it this way:

    nz[ ] = "hello" .

    otherwise your program won't work.

  4. #4
    Unregistered
    Guest
    Why Cant i separate

    nz[]="hello";

    into

    char nz[6];
    nz="hello";

  5. #5
    TK
    Guest
    Both are wrong.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I just told you. You can't assign an r-value to an l-value. If you need further explanation lookup those terms on google.com

  7. #7
    Unregistered
    Guest
    Program:
    #include <iostream.h>

    int main()
    {
    char *nz;
    nz="hello";

    cout << nz << "\n";
    return 0;
    }


    char *nz;
    nz="hello";

    isnt this also assign r-value to l-value?

  8. #8
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Why Cant i separate

    nz[]="hello";

    into

    char nz[6];
    nz="hello";
    I didn't mention that in your 2nd example, you need to initialize your string this way

    char nz[6]="hello"; or char nz[]="hello";

    You can't just use nz[] = "hello";

    char *nz;
    nz="hello";

    isnt this also assign r-value to l-value?
    Nope, that's assigning an r-value to an r-value.

  9. #9
    Unregistered
    Guest
    Wut Does l-value and r-value means?

    and

    why initialize it :char nz[6]="hello"; or char nz[]="hello";

    but not char nz[6];
    nz="hello"; <---- is that coz,r-value cant be assigned to an l-value?

  10. #10
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Originally posted by Unregistered
    Wut Does l-value and r-value means?


    http://msdn.microsoft.com/library/de...xpressions.asp


    and

    why initialize it :char nz[6]="hello"; or char nz[]="hello";
    I'm not sure why either, I only remember that's how the c-string is initialized. char nz[6] ="Hello"; is a string that can hold 5 characters, if you initialize to a string with more than 5 character, you'll get error messages. char nz[ ] = "hello" can hold as a string with many characters (undeterminated).

    but not char nz[6];
    nz="hello"; <---- is that coz,r-value cant be assigned to an l-value?
    You can't assign any value with this syntax nz="hello", because nz is a c-string (an array of character termintated by a null). And you have to use strcpy() function.

  11. #11
    Unregistered
    Guest
    l-value can not be Array

    but howcome this below is correct?

    char array[]="hello";

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    char *nz = "hello";
    Isn't exactly cool either. You should get in the habbit of allocating memory.

    Code:
    char *nz = new char[5];
    strcpy(nz, "hello");
    or

    Code:
    char *nz = (char *)malloc(5);
    strcpy(nz, "hello");
    Your compiler can interpret char *nz = "hello"; to mean one of many things. "hello" could be a static array of chars and nz is pointing to it (this one will run error free) or it could think "hello" is a temporary array of chars and nz points to it (this one will give you a good old seg fault). The fundamental difference between an array and a pointer is that an array has a finite size and a predictable location whereas a pointer is not. Before someone posts how wrong I am, I'll go ahead and point out that there are exceptions however a newbie would best understand what I just said.

  13. #13
    Registered User Unreg1stered's Avatar
    Join Date
    Jul 2002
    Posts
    25
    Originally posted by Unregistered
    l-value can not be Array

    but howcome this below is correct?

    char array[]="hello";
    Just an arays can be initialized, so alos can strings. The string can be intialized as the following exmaple:
    Code:
    char str[ ] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};  // notice that this string is ends up with a null character
    However, C concedes that strings are a special kind of character array by providing a shortcut:

    PHP Code:
    char str[ ] = "string";  // notice that we don't need to insert the null character 
    I hope that helps.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by master5001


    Isn't exactly cool either. You should get in the habbit of allocating memory.

    Code:
    char *nz = new char[5];
    strcpy(nz, "hello");
    or

    Code:
    char *nz = (char *)malloc(5);
    strcpy(nz, "hello");
    Your compiler can interpret char *nz = "hello"; to mean one of many things. "hello" could be a static array of chars and nz is pointing to it (this one will run error free) or it could think "hello" is a temporary array of chars and nz points to it (this one will give you a good old seg fault). The fundamental difference between an array and a pointer is that an array has a finite size and a predictable location whereas a pointer is not. Before someone posts how wrong I am, I'll go ahead and point out that there are exceptions however a newbie would best understand what I just said.
    Even these examples are not correct.
    Last edited by Troll_King; 07-21-2002 at 02:49 PM.

  15. #15
    Unregistered
    Guest
    The reason you can not separate it in to two lines is nz is constant, the first line is initialization and the second is assignment.

    can you do the following:

    const int i = 5;
    i = 55;

    same story.

    what do I know?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM