Thread: pointer and array

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whoie View Post
    Amazing . Not only do you misunderstand pointers and arrays, but you can't even do simple pointer arithmetic. I warned you to stop posting your nonsense, now you are exposing yourself worse.
    Perhaps you misunderstand me or require everything to be perfect wording or whatever. It's not nonsense, it's your mumbo jumo about being perfect, and I don't care what you say because everything I reply you turn back at me as if I'm sort of an idiot. Rude and ungrateful is what you are.

    You can cast anything to a pointer to char and access it that way. Not just 2d arrays, but everthing else. Because, every object can be viewed as an array of chars.
    But that does not mean an array can not be viewed as a pointer!

    The cast for the example that started this thread was to a (char **), which doesn't work. Only someone who thought, as you do, that arrays and pointers are the same would be confused by this. Thinking that arrays are really just pointers will only confuse you more, it doesn't help. This thread is a case in point.
    No, an array is a pointer, not an array of pointers. And there are always pitfalls with every line of thinking as nothing is perfect.
    And besides, thinking it's an array doesn't help because you can't get the size of an array after you passed it to a function, a feature specific to arrays.

    The point, is that it DOESN'T fetch the address to the array. It is already known, unlike dereferencing through a pointer. Your first sentence here is total nonsense. Your last sentence is one source of your confusion.
    Uhuh, right, so what if putting an offset into a register?
    Even if it may now work the same, on the surface, it is the same procedure.

    And whatever, you're trolling me and I'm not willing to reply to any of your replies anymore.
    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.

  2. #32
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by vart View Post
    array is r-value, pointer is l-value.
    On a side note, this is also wrong. In fact, an array cannot be an r-value - if I'm not mistaken, the only construct in C++ with this property. (Perhaps references, too.) When you try to convert an array to an r-value, it decays to a pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #33
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by Elysia View Post
    Perhaps you misunderstand me or require everything to be perfect wording or whatever. It's not nonsense, it's your mumbo jumo about being perfect, and I don't care what you say because everything I reply you turn back at me as if I'm sort of an idiot. Rude and ungrateful is what you are.
    I misunderstand you? Let's see what you said:

    Quote Originally Posted by Elysia View Post
    I disagree. An array is a pointer. Do you want to argue?
    Nope, that's pretty clear.

    Quote Originally Posted by Elysia View Post
    And whatever, you're trolling me and I'm not willing to reply to any of your replies anymore.
    Wrong again. I'm not trolling you, I'm flaming you. And pretty mildly at that. A troll posts something outrageous or inflammatory in order to start a flame war. Come to think of it, exactly who is trolling here?

  4. #34
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by whoie View Post
    Wrong again. I'm not trolling you, I'm flaming you. And pretty mildly at that. A troll posts something outrageous or inflammatory in order to start a flame war. Come to think of it, exactly who is trolling here?
    Well there's winning, and then there's beating a dead horse...
    Maybe it's time to close this thread since it's not going anywhere?

  5. #35
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Elysia View Post
    No, an array is a pointer, not an array of pointers.
    Once and awhile this discussion surfaces on the forum. Some books describe pointers and arrays as being identical. Stroustrup himself explains identical doesn't mean they are the same.

    An array is not a pointer. An array name instead decays into a pointer under certain circumstances. What this means is that the compiler generates a pointer to the first element of the array. Generates, is the word. The compiler needs to generate a pointer to access the array.

    A pointer to the first element of an array a is the equivalent of writing &a[0]. Pointers can access arrays and arrays are accessed by pointers.

    It then then follows, arrays are not pointers.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CornedBee
    On a side note, this is also wrong. In fact, an array cannot be an r-value - if I'm not mistaken, the only construct in C++ with this property. (Perhaps references, too.) When you try to convert an array to an r-value, it decays to a pointer.
    Going by the simple "left-hand side" versus "right-hand side" of an assignment" definition, your reasoning sounds correct. Yet the C++ Standard states:
    Quote Originally Posted by ISO/IEC 14882:2003 Section 4.2.1
    An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to an rvalue of type "pointer to T." The result is a pointer to the first element of the array.
    I cannot seem to figure out where it explicitly talks about rvalues of array type, so perhaps this is just a pre-caution for completeness or an oversight.
    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. #37
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    It probably only means that also p = a where p is a pointer and a an array. Which is essentially correct. However, 'a' is decayed to a pointer before the assignment takes place.
    Last edited by Mario F.; 02-12-2008 at 08:06 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #38
    Registered User
    Join Date
    Jan 2008
    Posts
    32
    that's a pretty healthy discussion..

    but can i know is there any way that i can do the following...
    Code:
    void myfunction(char buff**, int rows, int cols)
    and calling it like:
    Code:
    int main(void) {
    char inparr[4][4] = {0};
    myfunction(/*passing inparr to the function*/);
    }

    one the ways i found says that i have to atleast include the last dimenstion in the signature of the function.. i.e.
    Code:
    void myfunction(char buff[][4], int row, int col )
    but in that case there will be no use of the col argument
    how can i make sure that this function becomes generic and can include any kind of array of 2 dimensions

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't know in advance what the sizes are going to be, you'll have to "build" the array yourself, by changing buff[x][y] to buff[x*col+y], which is still (somewhat) an abuse of pointer notation, but one that people do all the time when passing arrays into functions. IOW, you'll have to do the behind-the-scenes work of arrays yourself. You can then pass inparr in to the function using &(inparr[0]), which should be a char**.

  10. #40
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    If you don't know in advance what the sizes are going to be, you'll have to "build" the array yourself, by changing buff[x][y] to buff[x*col+y], which is still (somewhat) an abuse of pointer notation, but one that people do all the time when passing arrays into functions. IOW, you'll have to do the behind-the-scenes work of arrays yourself. You can then pass inparr in to the function using &(inparr[0]), which should be a char**.
    But in that case all you really need to pass is a char* not a char**

  11. #41
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    But in that case all you really need to pass is a char* not a char**
    Good point! Good point! I think I was thinking of an array of "char-*-but-really-its-a-string". Right. You would only need to pass a ** if you were actually trying to build the array, by malloc'ing the memory.

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would only need to pass a ** if you were actually trying to build the array, by malloc'ing the memory.
    Since this is C++, we might use a char*& instead for such a situation. Then again, we might not use a null terminated string at all
    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. #43
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or we could use something no one will complain on --> vector arrays!
    std::vector< std::vector<std::string> > myarray;
    Now, on matter how you complain, it's always a vector or an array because it never decays into a pointer and you can always get the size. Plus it can also do bounds checking, so it's definitely an array and not a pointer!
    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.

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Or we could use something no one will complain on --> vector arrays!
    std::vector< std::vector<std::string> > myarray;
    The catch is that a vector of vectors is not guaranteed to be rectangular, so it may be better to use a container that enforces such a restriction if it is required.
    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

  15. #45
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or we could use something no one will complain on --> vector arrays!
    I will complain.

    To pass a 2d array as a double pointer, you have to build a pointer array:
    Code:
    int ar[4][4];
    int *arps[4] = { &ar[0][0], &ar[1][0], &ar[2][0], &ar[3][0] };
    func(arps, 4, 4);
    Better would be to pass a single pointer.
    Code:
    void func(int *ar2d, int rows, int cols);
    
    int ar[4][4];
    func(&ar[0][0], 4, 4);
    But anyway, when dealing with multi-dimensional arrays, nothing beats Boost.MultiArray.
    http://www.boost.org/


    laserlight, that's interesting. The 1997 draft doesn't mention rvalue arrays in that paragraph. So it definitely is something added afterwards, which means someone either was pedantic without thinking, or discovered a way to get an rvalue array.
    Let's see. The real distinction between lvalues and rvalues is that an lvalue's address can be taken. That's actually true for array variables, even though some compilers do the wrong thing there.
    You get rvalues by conversion from lvalues as per 4.1, but that excludes arrays. You can get rvalues by creating temporaries, but there's no way to create a temporary array: initializer lists aren't arrays, and a function can't return an array. Hmm ... you can't cast to an array. In particular, note that any cast that is not to a reference performs lvalue-to-rvalue or array-to-pointer conversion on its argument before casting, so you can't have a cast from an array either, unless it's a cast to "reference to array", which might be possible with reinterpret_cast, but results in an lvalue.
    So I think the rvalue array is a phantom, and the wording change a result of unthinking pedantery. Pedantism. Whatever.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

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