*char and char

This is a discussion on *char and char within the C++ Programming forums, part of the General Programming Boards category; Two questions really, What is the difference between declaring *char Name, and char Name, and what are the advantages and ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    *char and char

    Two questions really,
    What is the difference between declaring *char Name, and char Name, and what are the advantages and disadvantages of using either declaration?
    Thanks.
    Be a leader and not a follower.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I suppose you meant :

    char* Name and char Name

    The one with the asterisk(*), is a pointer to a char, which can hold the address of another char variable.

    The one without the asterisk(*), is a normal char variable which can hold characters.

    To clarify, both char* and char variables hold data, the difference is that the data for the char* variable, must be the address of a normal char variable.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    ok, so do these two declarations have exactly the same affect?

    char *FirstName="Dene", char FirstName[]="Dene\0";
    Be a leader and not a follower.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Same effect, but remember that the pointer can also point somewhere else.

    Eg.
    Code:
    char*  ptr;
    char    Name[]      = "TestName";
    char    Surname[] = "TestSurname";
    
    ptr = Name;
    cout << ptr;
    ptr = Surname;
    cout << ptr;
    btw you don't need to put the \0 in the initial string declaration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21