![]() |
| | #1 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| 2D char array and pointer Code: #include <stdio.h>
void testfunc(char **ray) {
printf("%c\n",ray[2][2]);
}
int main () {
char *ray[5][12]={"this","that","and","more"};
testfunc((char**)ray);
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| No, because a char ** is not the same as a char [5][12] or some such. They may appear similar, but when you pass the pointer to char[5][12], you are actually passing a pointer to (*char)[12], not the address of an array of pointers to char. In this case, you must supply all but the left-most size, e.g. Code: void testfunc(char ray[][12]) {
printf("%c\n",ray[2][2]);
}
-- 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. |
| matsp is offline | |
| | #3 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Further, this is an array of const char* pointers, not a 2D array, which makes it even more wrong. Of course, since it's an array of pointers, it just happens to work: Code: #include <stdio.h>
void testfunc(const char** ray)
{
printf("%c\n", ray[2][2]);
}
int main ()
{
const char* ray[] = { "this", "that", "and", "more" };
testfunc(ray);
return 0;
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #4 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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. | |
| matsp is offline | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Quote:
What I am mostly worried about is the notation *ray[5][12] implies I could actually have a 3Dish array (a 2D pointer array) and that the "elements" to which I am assigning strings may have sizeof(char*) and not 12. For example, I can now compile this with char *ray[5][12]: Code: printf("%s\n",ray[0][0][0]);
SInce I don't seem to be producing an overflow (no GUI debugger...) with it, apparently *ray[5][12] is really a 2D char array (a "list") with a pointer to it? Just using ray[5][12] makes a **pointer impossible. To make a long story short: you don't see a potential overflow in main(), just a possible exceeding of boundaries in testfunc() with the number of elements? I'm guessing this is because it's easier for the compiler to use an array of pointers with 12 bytes assigned to each rather than to use a pointer to an array of elements with a size of 12 each. So there is no pointer to such a thing possible. ps. I really really want **char as the argument to testfunct()
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #6 | |||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
Quote:
The actual type should be char (*)[5]. Code: #include <stdio.h>
void testfunc(char (*ray)[5])
{
printf("%c\n", ray[2][2]);
}
int main ()
{
char ray[12][5] = {"this", "that", "and", "more"};
testfunc(ray);
return 0;
}
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 02-10-2009 at 02:35 PM. | |||
| Elysia is offline | |
| | #7 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Sorry, I missed the (very obvious) red star in the original post. Still, a 2D array is not a pointer to pointer. If you want to do that, then do what Elysia says. If you want a 2D array of char, and need a pointer to that [e.g. you want to modify the content of the strings in an array of strings], then you need to pass the length of the strings. -- 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. |
| matsp is offline | |
| | #8 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| That's it. I just noticed it functions as a 3D array in main.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #9 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I imagine yours does, because array are laid out sequentially in memory. It just happens to work, but I would definitely say it's undefined. EDIT: It might just work because you have a pointer to the array (all arrays, 2D or whatever are essentially just one pointer anyway), and as the array is storing pointers, you have an extra pointer in the type and it... works. But if you change it to a 2D array or 3D array you will get trouble.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
Last edited by Elysia; 02-10-2009 at 02:50 PM. | |
| Elysia is offline | |
| | #10 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Hmmm...okay I just noticed this seems to work very nicely, it's only slightly more limited but it still saves a lot of potential malloc and strcpy. I suppose it is the same as just using char[][12] except I can use actual pointers in testfunc() (rather than a local copy): Code: #include <stdio.h>
#include <string.h>
typedef struct {
char X[256];
} string;
void testfunc(string *ray) {
printf("%s\n",(char*)&ray[2]);
strcpy((char*)&ray[3],"well done");
}
int main () {
string ray[4]={"this and that","more and","hello world"};
testfunc(ray);
printf("%s\n",(char*)&ray[3]);
return 0;
}
Code: test2.c: In function ‘main’: test2.c:14: warning: missing braces around initializer test2.c:14: warning: (near initialization for ‘ray[0]’)
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #11 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| I believe it must be: Code: string ray[4] = { { "this and that" }, etc };
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #12 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| You can get rid of the warning by doing: Code: string ray[4]={{"this and that"},{"more and"},{"hello world"}};
-- 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. |
| matsp is offline | |
| | #13 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Thanks again people.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #14 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Perhaps you meant to print the string instead of the character it points to - and here's my 2c. Code: #include <stdio.h>
void testfunc(const char *ray[][12])
{
printf("%s\n",ray[0][2]);
}
int main()
{
const char *ray[5][12]={"this","that","and","more"};
testfunc(ray);
return 0;
}
|
| itCbitC is offline | |
| | #15 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,945
| Perhaps you should have -- nevermind. Anyway all the code I posted works in the way I describe it as working, I wasn't asking for that kind of fix.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
![]() |
| Tags |
| 2d char, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic array of structures containing yet another dynamic array of structures | innqubus | C Programming | 2 | 07-11-2008 07:39 AM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Quick question about SIGSEGV | Cikotic | C Programming | 30 | 07-01-2004 07:48 PM |
| Struct *** initialization | Saravanan | C Programming | 20 | 10-09-2003 12:04 PM |
| convert long to pointer to char array | gazmack | C++ Programming | 5 | 09-26-2003 11:33 AM |