Thread: array[0] != array + 0 ???

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    array[0] != array + 0 ???

    I have this line:

    PHP Code:
    printf("c[bk]: %c \t c+bk: %c\n",camadas_dados[bk],camadas_dados+bk); 
    But when it prints out, i receive this:


    http://xs125.xs.to/xs125/08141/lol647.jpg


    Why are they different? How can i make them equal?

    Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The subscript operator dereferences the address
    camadas_dados[bk]
    is equivalent to
    Code:
    *( camadas_dados + bk )
    My best code is written with the delete key.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    array[0] is the first element of the array. array + 0 is a pointer to the first element of the array. Clearly, a value and a pointer to that value are fundamentally different.
    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

  4. #4
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Thanks!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is,
    camadas_dados[bk]
    Should actually be
    &camadas_dados[bk]
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is,
    camadas_dados[bk]
    Should actually be
    &camadas_dados[bk]
    I think it depends on what Milhas actually wants to do, e.g., from the example I would say that camadas_dados + bk should be *(camadas_dados + bk) as in Prelude's example.

    Also, why use &camadas_dados[bk] instead of camadas_dados + bk? It seems to me that if bk is equal to the number of elements of the array, camadas_dados + bk would result in a one past the end pointer, but &camadas_dados[bk] would take the address of a non-existent object.
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Also, why use &camadas_dados[bk] instead of camadas_dados + bk? It seems to me that if bk is equal to the number of elements of the array, camadas_dados + bk would result in a one past the end pointer, but &camadas_dados[bk] would take the address of a non-existent object.
    Whilst technically there may be a subtle difference, if we are talking char arrays (or any other plain old data/C types), rather than some C++ type where [] is overloaded with a function, &camadas_dados[bk] and camadas_dados + bk would generate exactly the same code [and both produce the address of the element immediately after the array memory provided if bk == number of array elements. The only difference is style.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oops, yes, you're right in that it wasn't supposed to take the address.
    The correct statement would be that
    camadas_dados + bk
    Is the same as
    &camadas_dados[bk]

    And
    camadas_dados[bk]
    Is the same as
    *(camadas_dados + bk)

    Which one to use is purely up to the programmer.
    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.

  9. #9
    Banned
    Join Date
    Mar 2008
    Posts
    78
    I Want atoi to do this:

    atoi(camadas_dados[bk]);

    but it gives me warnings at compilation.

    atoi(*(camadas_dados+bk)); gives the same warning

    atoi(&camadas_dados[bk]); returns strange addresses.


    Edit: And atoi(camadas_dados+bk); returns Zero everytime

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The last line is correct. But as to the problem, it can be many things. It's better to show the code so we can do a diagnostic.
    (Why is it correct? Because atoi wants a pointer to char [char*], and [] derferences the pointer [or array] [and the same with *], so putting a & [address of operator] there takes the address from that element forwards.)
    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.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Milhas View Post
    I Want atoi to do this:

    atoi(camadas_dados[bk]);

    but it gives me warnings at compilation.

    atoi(*(camadas_dados+bk)); gives the same warning

    atoi(&camadas_dados[bk]); returns strange addresses.
    Assuming that camadas_dados is declared as:
    Code:
    char camadas_dados[X];
    then this form is the correct one.
    Code:
    atoi(&camadas_dados[bk]);
    // or
    atoi(camadas_dados + bk);
    If you get strange results from that, perhaps your string isn't correctly terminated, or the string is not a number in some way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    &camadas_dados[bk] and camadas_dados + bk would generate exactly the same code [and both produce the address of the element immediately after the array memory provided if bk == number of array elements.
    In practice, I think that is what will happen, but semantically speaking, I thought that &camadas_dados[bk], where bk is equal to the number of elements of camadas_dados, is undefined.

    However, the C99 Standard states:
    Quote Originally Posted by Section 6.5.3.2
    The unary & operator returns the address of its operand. (...) Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator.
    So I am wrong to think that &camadas_dados[bk] would take the address of a non-existent object under the circumstances described.
    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

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    In practice, I think that is what will happen, but semantically speaking, I thought that &camadas_dados[bk], where bk is equal to the number of elements of camadas_dados, is undefined.

    However, the C99 Standard states:


    So I am wrong to think that &camadas_dados[bk] would take the address of a non-existent object under the circumstances described.
    But as soon as you USE the pointer produced by camdados_dados + bk, you are in an equally "undefined behaviour" situation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Banned
    Join Date
    Mar 2008
    Posts
    78
    If i have char array[3] = "123"

    and give atoi(&array[1]), atoi will give me int '2' or int '23' ?

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It will return 23 since &array[1] is the address starting at the second element, which is the "2".
    Atoi will the scan until it finds the end of the string, which is after the "3". So it will return 23.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM