Author |
Message |
Pola #1 / 14
|
 memory allocation for pointer to pointer to char
I have char **pptochar and i want to allocate a memory segment like pp[3][5] array in the memory |-----|-----|-----| please help me if it is possible and tell me, a book or tutorial about mastering pointers. Sent via Deja.com http://www.*-*-*.com/ Before you buy.
|
Wed, 18 Sep 2002 03:00:00 GMT |
|
 |
mike burrel #2 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> I have char **pptochar and i want to allocate a memory segment > like pp[3][5] array in the memory |-----|-----|-----|
most likely your problem is due to the memory |-----|-----|-----| being previously allocated by the operating system. it might also be of interest that pptochar is a pointer to (a) pointer(s), not any sort of array to characters. let me draw you a diagram: here's what you want: pp[0] | pp[1] | pp[2] _____________________________ pp -> | | | | | | | | | | | | | | | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ where each of the cute wittle boxen are characters here's what you'll get: _________ pp +> pp[0] -> | | | | | | | ^^^^^^^^^ | _________ +> pp[1] -> | | | | | | | ^^^^^^^^^ | _________ +> pp[2] -> | | | | | | ^^^^^^^^^ there is a huge distinction. -- /"\ m i k e b u r r e l l
X AGAINST HTML MAIL http://mikpos.dyndns.org / \
|
Wed, 18 Sep 2002 03:00:00 GMT |
|
 |
Mark McIntyr #3 / 14
|
 memory allocation for pointer to pointer to char
Quote: >I have char **pptochar and i want to allocate a memory segment >like pp[3][5] array in the memory |-----|-----|-----| >please help me if it is possible and tell me, >a book or tutorial about mastering pointers.
Look up malloc, and do something like this: allocate pp enough memory for 3 pointers to char for each of the three pointers, allocate enough memory for 5 chars. thats it Mark McIntyre C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
|
Wed, 18 Sep 2002 03:00:00 GMT |
|
 |
espri #4 / 14
|
 memory allocation for pointer to pointer to char
array and pointer is processed internally in the same way, (but array name is not same object with the point name) so first, you allocate (column)*(row)*(object type size) to pointer then use array form as you like example ---------------------------- #define COLUMN 3 #define ROW 5 typedef OBJ_TYPE char . . . OBJ_TYPE *ch; ch=malloc(COLUMN*ROW*sizeof(OBJ_TYPE)); ch[0][0]='1'; ch[0][1]='\0'; ch[1]="ARM"; // not exceed ROW . . . * Sent from RemarQ http://www.remarq.com The Internet's Discussion Network * The fastest and easiest way to search and participate in Usenet - Free!
|
Wed, 18 Sep 2002 03:00:00 GMT |
|
 |
Pola #5 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> >I have char **pptochar and i want to allocate a memory segment > >like pp[3][5] array in the memory |-----|-----|-----| > >please help me if it is possible and tell me, > >a book or tutorial about mastering pointers. > Look up malloc, and do something like this: > allocate pp enough memory for 3 pointers to char > for each of the three pointers, allocate enough memory for 5 chars. > thats it > Mark McIntyre > C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
i have tried *pp=(char *)malloc(n*sizeof(char)) *pp=(char *)calloc(3,5) ,but both of them does not work while assigning a character to pp like a two dimensional. it gives Seg.Fault. Sent via Deja.com http://www.deja.com/ Before you buy.
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Mal Ka #6 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> I have char **pptochar and i want to allocate a memory segment > like pp[3][5] array in the memory |-----|-----|-----|
To arrive at the two dimensional array laid out in memory as you illustrate then you will need a pointer of a type different from char ** to reference the region. In words you need a pointer to an array of 5 characters; which can be somewhat confusing to code directly so let us define a type: typedef char ARRAY5[5]; now we define pptochar as: ARRAY5 *pptochar; now to get the allocation: pptochar=malloc(3*sizeof(*pptochar)); if(pptochar==NULL) { /*error recovery or message and abort*/ } This should allocate 15 contiguous bytes of memory addressable in the form pptochar[i][j]; Malcolm Kay
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Mal Ka #7 / 14
|
 memory allocation for pointer to pointer to char
Quote:
...... > > allocate pp enough memory for 3 pointers to char > > for each of the three pointers, allocate enough memory for 5 chars. ........ > i have tried > *pp=(char *)malloc(n*sizeof(char)) > *pp=(char *)calloc(3,5)
You are trying to store a pointer returned by malloc/calloc at the position in memory pointed to by pp. But pp doesn't yet point to anywhere in particular. If you carefully read Mark's suggestion and study how malloc works you will end up with something along along the lines of: char **pp; int i; pp=malloc(3*sizeof(*pp)); /* sets up space for 3 pointers */ if(pp==NULL) /* handle error */; for(i=0;i<3;i++) /* set each pointer to point to 5 char space */ { pp[i]=malloc(5*sizeof(**pp)); if(pp[i]==NULL) /* handle error */; } Note that casting the return from malloc is frowned upon in C as it hides programming errors. If your compiler objects you've done something wrong. Note that this approach does not cause pp to point to 15 bytes of contiguous allocated memory, but points to 3 pointers that each point to a region of 5 allocted characters. The allocated space can be accessed with pp[i][j] but the physical layout in memory is not that which you asked for. Malcolm Kay
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Pola #8 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> > I have char **pptochar and i want to allocate a memory segment > > like pp[3][5] array in the memory |-----|-----|-----| > To arrive at the two dimensional array laid out in memory as you > illustrate then you will need a pointer of a type different from > char ** to reference the region. > In words you need a pointer to an array of 5 characters; which > can be somewhat confusing to code directly so let us define a type: > typedef char ARRAY5[5];
Thank you very much. I have been trying to find how do it for 1 week. Quote: > now we define pptochar as: > ARRAY5 *pptochar; > now to get the allocation: > pptochar=malloc(3*sizeof(*pptochar)); > if(pptochar==NULL) { > /*error recovery or message and abort*/ > } > This should allocate 15 contiguous bytes of memory addressable > in the form pptochar[i][j]; > Malcolm Kay
Sent via Deja.com http://www.deja.com/ Before you buy.
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Mark McIntyr #9 / 14
|
 memory allocation for pointer to pointer to char
Quote:
>> >I have char **pptochar and i want to allocate a memory segment >> >like pp[3][5] array in the memory |-----|-----|-----| >> >please help me if it is possible and tell me, >> >a book or tutorial about mastering pointers. >> Look up malloc, and do something like this: >> allocate pp enough memory for 3 pointers to char >> for each of the three pointers, allocate enough memory for 5 chars. >i have tried >*pp=(char *)malloc(n*sizeof(char)) >*pp=(char *)calloc(3,5) >,but both of them does not work >while assigning a character to pp >like a two dimensional. it gives Seg.Fault.
You're in the right area but you need to reread what I said 0) Assuming you defined pp correctly: char **pp=NULL; 1) allocate pp enough memory for 3 pointers to char pp =malloc(3 * sizeof(char*)); 2) for each of the three pointers, allocate enough memory for 5 chars. pp[0] = malloc(5); pp[1] = malloc(5); pp[1] = malloc(5); Mark McIntyre C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Mal Ka #10 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> ---------------------------- > #define COLUMN 3 > #define ROW 5 > typedef OBJ_TYPE char
Why define a new type for char? Is this some form of misdirection to make later errors appear more plausible? Quote: > . > . > . > OBJ_TYPE *ch; > ch=malloc(COLUMN*ROW*sizeof(OBJ_TYPE)); > ch[0][0]='1';
In reality ch is a pointer to char. Thus it can ony have a single subscript (or array index if you prefer). In any case how does the compiler know to attribute ROW as the dimension of the second index. Quote: > ch[0][1]='\0'; > ch[1]="ARM"; // not exceed ROW
ch[1] only has space for a character say 'A', not a string or a string pointer. Malcolm Kay
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Pola #11 / 14
|
 memory allocation for pointer to pointer to char
Is there any tutorial or book about mastering pointers in C ?. Thanks in advice. Sent via Deja.com http://www.deja.com/ Before you buy.
|
Thu, 19 Sep 2002 04:00:00 GMT |
|
 |
Mal Ka #12 / 14
|
 memory allocation for pointer to pointer to char
Quote:
> Is there any tutorial or book about mastering pointers in C ?.
I don't think a book specifically on pointers in C would be a very good idea. Pointers are at the very core of the C language concept and shouldn't (in my opinion) be separated from the rest of the C language. I guess any of the well reputed books on the language are OK. My principal introduction to C was via K&R 1 i.e. The C Programming Language by Kernigan and Richie first edition; (I hope I've spelt the names correctly) which I found extremely readable. That first edition is now seriously out of date but the second edition (K&R 2) is still one of the best C tutorials and references. Understanding pointers is mostly about studying examples and thinking through the underlying meaning of each statement. In my opinion a large part of the understanding comes from recognising what information the compiler needs to turn the C source code into assembly or machine code. (At times I've claimed only partly tongue in cheek that if you can write it unambiguously in C it must be OK) Become a code reader - read and try to understand the sources of other people's programs that are known to work well. You don't need to come to grips with all the source of a large application; if it is well written you'll find that there are many modules which are understandable more or less in isolation. But don't ever believe that anyone always produces good or even correct code. Malcolm Kay
|
Fri, 20 Sep 2002 03:00:00 GMT |
|
 |
Shagg #13 / 14
|
 memory allocation for pointer to pointer to char
Groovy hepcat Polat was jivin' on Sat, 01 Apr 2000 21:20:52 GMT in comp.lang.c. memory allocation for pointer to pointer to char's a cool scene! Dig it! Quote: >I have char **pptochar and i want to allocate a memory segment >like pp[3][5] array in the memory |-----|-----|-----|
Have a look at my 2D allocation functions which I posted here recently. -- ----- Dig the EVEN NEWER, MORE IMPROVED news sig!! ----- -------------- Shaggy was here! --------------- http://aardvark.apana.org.au/~phaywood/ ============= Ain't I'm a dawg!! ==============
|
Mon, 23 Sep 2002 03:00:00 GMT |
|
 |
amarli.. #14 / 14
|
 memory allocation for pointer to pointer to char
Quote: > I have char **pptochar and i want to allocate a memory segment > like pp[3][5] array in the memory |-----|-----|-----| > please help me if it is possible and tell me, > a book or tutorial about mastering pointers. > Sent via Deja.com http://www.deja.com/ > Before you buy.
I will let you know a book to master pointers...Here it is... "Pointers using C" by Yeshwant Kanetkar. (The book is authored by an Indian person) ---Amar :-) Sent via Deja.com http://www.deja.com/ Before you buy.
|
Mon, 23 Sep 2002 03:00:00 GMT |
|
|