Thread: A question about pointers and arrays

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    10

    A question about pointers and arrays

    As I'm reading "Jumping into C++" and liking the way it is written, I've come across a point that I don't think I understand properly. It says:

    assigning an array to a pointer:

    Code:
    int numbers [8];
    int *p_numbers = numbers;
    using it like an array:
    Code:
    for (int i = 0; i < 8; i++)
    {
    p_numbers[i] = i;
    }
    Here's the confusing part:
    "The array numbers, when assigned to a pointer, acts as though it is just a pointer. Arrays are not pointers, but can be assigned to pointers."

    Am I missing something here? Because it looks to me that the pointer p_numbers acts as an array not the other way around... Please if somebody can clarify this...

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Line 2 - what is the type of numbers? It's int[8].What is the type of p_numbers? It's int*. Do they differ? Yes. And yet - you can make that assignment. That's what the book is addressing by "Arrays are not pointers, but can be assigned to pointers."

    Now, what happens "under the hood" is that if in certain (you could say most) contexts you drop an array name like that - it "decays" to a pointer, or in other words: the array-of-T gets converted to a pointer-to-T that points at the first element of the array.

    Here, a bit of standardese:
    4.2 Array-to-pointer conversion [conv.array]
    1 An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.
    Is it gibberish to you? Even if, take what's important out of it, and realize that there's a lot to be learned about the nitty gritty that you can for the moment ignore.

    About the brackets, again, standardese bit:
    5.2.1 Subscripting [expr.sub]
    1 [...] One of the expressions shall have the type “array of T” or “pointer to T” and the other shall have unscoped enumeration or integral type. The result is of type “T”. The type “T” shall be a completely-defined object type.
    The expression E1[E2] is identical (by definition) to *((E1)+(E2))
    So you could say that [] working with arrays and pointers is actually just a syntactic sugar. That's also why stuff like this works:
    Code:
    int a[5] = { 0 };
    1[a] = 5; // <-- good job obfuscating your code. ;)

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IMO, I would strongly suggest you get a better book (e.g. Accelerated C++), because this book do your a disfavour by trying to learn you all kinds of stuff first that you later have to unlearn.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    Quote Originally Posted by Xupicor View Post
    Line 2 - what is the type of numbers? It's int[8].What is the type of p_numbers? It's int*. Do they differ? Yes. And yet - you can make that assignment. That's what the book is addressing by "Arrays are not pointers, but can be assigned to pointers."
    Thank you, it's much clearer, but this second part with the brackets isn't so for now I'm gonna ignore it :P

  5. #5
    Registered User
    Join Date
    Sep 2016
    Posts
    10
    Thanks for the suggestion, I'll have a look

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question in arrays and pointers
    By ASophokleous in forum C Programming
    Replies: 3
    Last Post: 10-27-2013, 11:35 AM
  2. Pointers and multidimensional arrays question
    By d2rover in forum C Programming
    Replies: 10
    Last Post: 11-07-2010, 10:43 AM
  3. Beginner pointers/arrays question
    By whiteandnerdy in forum C Programming
    Replies: 3
    Last Post: 07-14-2010, 02:19 PM
  4. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  5. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM

Tags for this Thread