Thread: Array Syntax Versus Pointer Syntax

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Array Syntax Versus Pointer Syntax

    From what I've read, the following two definitions do the same thing:

    Code:
    char str[] = "Example string";
    char* str = "Example string";
    But what about when you want to define a string of a specific length, like this:

    Code:
    char str[20];
    Is it possible do this with pointer syntax? Or would I have do to something like this:

    Code:
    char string[20];
    char* str = string;

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Those are not the same:

    Code:
    char str[] = "Example string";  //the type of str is char[15]
    char* str = "Example string";  //the type of str is char*
    Furthermore, in the second case it's a char* to an immutable string array. It should be declared as const char*, and the compiler lets you get away without the const only for obscure legacy reasons.

    Code:
    char string[20];
    char* str = string;
    Yes, here str is a pointer to the first character in the array of 20 characters called string.
    Last edited by anon; 12-05-2011 at 04:05 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by anon View Post
    Those are not the same:

    Code:
    char str[] = "Example string";  //the type of str is char[15]
    char* str = "Example string";  //the type of str is char*
    Right, I realize that one is an array and one is a pointer. What I meant is that they both define strings set to the same thing, even if one is the string itself and one is a pointer to it.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by LyTning94 View Post
    Is it possible do this with pointer syntax? Or would I have do to something like this:

    Code:
    char string[20];
    char* str = string;
    If that's literally what you want, then yes. To declare a pointer to the beginning of some fixed-size buffer, you would do the above.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by LyTning94 View Post
    Is it possible do this with pointer syntax? Or would I have do to something like this:

    Code:
    char string[20];
    char* str = string;
    It is not possible to use some special pointer syntax to indicate size because a pointer is just that - it points to something. It has no notion of size.
    The above is only way to do what you want if you need a fixed-size buffer and a pointer to it (but why would you want a pointer to it?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by Elysia View Post
    (but why would you want a pointer to it?).
    In the example I was reading, it used a pointer to a string so it could increment and loop through the characters, like this:

    Code:
    char string[20];
    char* str = string;
    
    cin.get(str, 20);
    
    for (int i = 0; i < 20; i++)
    {
         cout<< *(str++) << endl;
    }
    but I guess you could just do this instead:

    Code:
    char string[20];
    
    cin.get(string, 20);
    
    for (int i = 0; i < 20; i++)
    {
         cout<< *(string+i) << endl;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LyTning94
    but I guess you could just do this instead:
    You could, but then you might as well write string[i] instead of *(string+i). Incidentally, be careful about using the name string in case you conflict with std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax confusion with array/pointer
    By ceddsoboss in forum C Programming
    Replies: 10
    Last Post: 11-22-2010, 06:13 PM
  2. Basic pointer syntax
    By Mnemonist in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2010, 10:37 PM
  3. Help with pointer syntax
    By ajingham in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 07:15 PM
  4. Origin of pointer syntax
    By dudeomanodude in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2008, 01:35 PM
  5. Pointer Syntax
    By monkey_C in forum C Programming
    Replies: 7
    Last Post: 09-13-2002, 03:36 AM