Thread: strings Vs. Char pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so we know you can't just pass a 2D array as array**. That won't work.
    However, even so, you are accessing the array just like a 2D-pointer. That, in my book, makes it an array.
    I don't know why the standard requires you to specify the inner/outmost dimension, whichever it is, when passing 2D-arrays. I just don't. But it doesn't change the fact that after the function that takes the arguments acquires that variable, it's just like a pointer. Again.

    So, arrays are just pointers with special syntax. Makes sense if you think about it.
    I don't think you should teach arrays and pointers as different things. I believe in that teaching that arrays are pointers with special syntax. Because if you know how to use pointers, then you also know how arrays works.
    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    However, even so, you are accessing the array just like a 2D-pointer.
    That's not really true: If a is defined as int a[10][10], a does not get passed into a function as a pointer to pointer, but a pointer to an array. (Array decay does not happen recursively, in other words.)

    Quote Originally Posted by Elysia View Post
    I don't know why the standard requires you to specify the inner/outmost dimension, whichever it is, when passing 2D-arrays.
    I believe you, as breathtaking an admission as that is. Think for a minute: If you had a[10][10], so you needed to store 100 integers in memory somewhere, how would you arrange it so you could find a[0][1] and a[1][0] easily?

    Right: you'd store things in rows, so that all the a[0][m]'s appear first, then all the a[1][m]'s next, and so on. In fact, you would know that the offset for a[x][y] would be 10*x+y (times the size of an int, of course). Now, if all you had was a bare pointer to the first element, how would you find a[1][0]?

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by tabstop View Post
    Now, if all you had was a bare pointer to the first element, how would you find a[1][0]?
    The same way that you would know how to find a[5] in a 1D array. You'd need to pass the size of the array to the function as well as the array. So if you pass a 2D array, I'm not sure why a pointer-to-pointer doesn't work since you could just pass 2 sizes along with the pointer?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cpjust View Post
    The same way that you would know how to find a[5] in a 1D array. You'd need to pass the size of the array to the function as well as the array. So if you pass a 2D array, I'm not sure why a pointer-to-pointer doesn't work since you could just pass 2 sizes along with the pointer?
    Which is why you need to pass a 2-D array as a[][10], just so that you can pass the size along with the pointer. If all you have is a pointer (which is all you get with int **), you don't have the size of the array there.

    The point I'm (failing to) make here is that int ** and int[][10] are not the same type at all, since the first is "just a" pointer, while the second is a pointer plus enough information to make actually using the thing work.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Hmm, I've never thought of this stuff the way you guys(cas especially) are putting it.

    So, if I'm getting this right:

    If you take the address of a char * variable initialized as a string literal, you'd get the address of the pointer variable, but if you'd take the address of an array, you'd just get the address of the first element, which in fact means that there is no real variable 'storing' the address, so to speak?

    And if my thoughts are on the right track, does someone care to explain how the sizeof macro works on arrays, when it doesn't on pointers?

    I do understand the difference between the two more thoroughly now, but I still can't think of a way how the macro can work?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by IceDane View Post
    Hmm, I've never thought of this stuff the way you guys(cas especially) are putting it.

    So, if I'm getting this right:

    If you take the address of a char * variable initialized as a string literal, you'd get the address of the pointer variable, but if you'd take the address of an array, you'd just get the address of the first element, which in fact means that there is no real variable 'storing' the address, so to speak?

    And if my thoughts are on the right track, does someone care to explain how the sizeof macro works on arrays, when it doesn't on pointers?

    I do understand the difference between the two more thoroughly now, but I still can't think of a way how the macro can work?
    The first paragraph is basically right. If I declare int a[10][10], a (by itself) will generally decay to a pointer to first element, and &a must mean the address of (the start of) the array, so they'll give you the same value. I usually think of the variable a as storing the address and a[m][n] the actual data.

    Sizeof is an operator that works on types; if you give it an expression, it is converted to its type first. So sizeof(int *) gives you the size of a pointer, sizeof(int[10]) gives you the size of a 10-element array of integers. Edit to add: And here's where the difference between a and &a comes in: with the declaration above, sizeof(a)==400 and sizeof(&a)==4 (at least on my machine, et cetera).
    Last edited by tabstop; 02-02-2008 at 09:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying pointers
    By Tom Bombadil in forum C Programming
    Replies: 10
    Last Post: 05-22-2009, 01:26 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM