Author |
Message |
mar #1 / 15
|
 dynamic array of pointers
Hello- i know it's possible to use an array of pointers i.e. char *array[25]; i use this all the time. but, is it possible to make a dynamic array of pointers i.e. use malloc to first make a pointer and then malloc again to make an array of pointers? what would be the syntax? i would appreciate any comments or references to K&R (i couldn't find it in there but thats not to say its not there)... thanks. mark. :-)
|
Wed, 02 Feb 2005 22:17:36 GMT |
|
 |
Kevin Easto #2 / 15
|
 dynamic array of pointers
Quote:
> Hello- > i know it's possible to use an array of pointers i.e. char > *array[25]; i use this all the time. but, is it possible to make a > dynamic array of pointers i.e. use malloc to first make a pointer and > then malloc again to make an array of pointers? what would be the > syntax? i would appreciate any comments or references to K&R (i > couldn't find it in there but thats not to say its not there)... > thanks. > mark. :-)
char **ptrarray = malloc(sizeof(char *) * n); now ptrarray points to a dynamically allocated contiguous block of char pointers. You can just access them as you would elements of the array in your example - just remember to free it when it's no longer necessary. - Kevin.
|
Wed, 02 Feb 2005 22:26:18 GMT |
|
 |
Malcol #3 / 15
|
 dynamic array of pointers
Quote: > Hello- > i know it's possible to use an array of pointers i.e. char > *array[25]; i use this all the time. but, is it possible to make a > dynamic array of pointers i.e. use malloc to first make a pointer and > then malloc again to make an array of pointers? what would be the > syntax? i would appreciate any comments or references to K&R (i > couldn't find it in there but thats not to say its not there)... > thanks. > mark. :-)
#define MAXNAMELEN 128 char **namelist; int Nnames = 100; namelist = malloc( sizeof(char *) * Nnames); if(!namelist) exit(EXIT_FAILURE); for(i=0;i<Nnames;i++) { namelist[i] = malloc(MAXNAMELEN); if(!namelist[i]) exit(EXIT_FAILURE); sprintf(namelist[i], "Fred%d", i+1); Quote: }
This will produce an array of names initilaised to Fred1, Fred2 ... etc
|
Wed, 02 Feb 2005 22:33:54 GMT |
|
 |
insi #4 / 15
|
 dynamic array of pointers
"mark" : i know it's possible to use an array of pointers i.e. char : *array[25]; i use this all the time. but, is it possible to make a : dynamic array of pointers i.e. use malloc to first make a pointer and : then malloc again to make an array of pointers? Yes e.g. char * * p = malloc(sizeof * p * 8); if (p) { char * * q = p + 8; while (q != p) if (* -- q = malloc(sizeof * q * 256)) * * q = '0', * q [1] = 0; Quote: }
|
Wed, 02 Feb 2005 22:49:25 GMT |
|
 |
insi #5 / 15
|
 dynamic array of pointers
: "mark" : : i know it's possible to use an array of pointers i.e. char : : *array[25]; i use this all the time. but, is it possible to make a : : dynamic array of pointers i.e. use malloc to first make a pointer and : : then malloc again to make an array of pointers? : : Yes e.g. : : char * * p = malloc(sizeof * p * 8); : if (p) { : char * * q = p + 8; : while (q != p) : if (* -- q = malloc(sizeof * q * 256)) sorry this should read if (* -- q = malloc(sizeof * * q * 256)) : * * q = '0', * q [1] = 0; : } : : :
|
Wed, 02 Feb 2005 22:51:32 GMT |
|
 |
Mark McIntyr #6 / 15
|
 dynamic array of pointers
On Sat, 17 Aug 2002 14:26:18 +0000 (UTC), in comp.lang.c , Kevin Quote:
>> Hello- >> i know it's possible to use an array of pointers i.e. char >> *array[25]; i use this all the time. but, is it possible to make a >> dynamic array of pointers i.e. use malloc to first make a pointer and >> then malloc again to make an array of pointers? what would be the >> syntax? i would appreciate any comments or references to K&R (i >> couldn't find it in there but thats not to say its not there)... >> thanks. >> mark. :-) >char **ptrarray = malloc(sizeof(char *) * n);
thats a slightly confusing way round to write it, at first glance I read that as sizeof *n cast to a char*. Also the gurus here would recommend using sizeof the object pointed to rather than sizeof a type. char ** ptrarray = malloc(n * sizeof *ptrarray); -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Wed, 02 Feb 2005 23:24:04 GMT |
|
 |
Mark McIntyr #7 / 15
|
 dynamic array of pointers
On Sat, 17 Aug 2002 16:51:32 +0200, in comp.lang.c , "insin" Quote:
>if (* -- q = malloc(sizeof * * q * 256))
whazzat * -- q ? You might need some parens there, to make it unambiguous to the reader, plus its as easy to simply use array notation surely? -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Wed, 02 Feb 2005 23:26:06 GMT |
|
 |
insi #8 / 15
|
 dynamic array of pointers
"Mark McIntyre" : On Sat, 17 Aug 2002 16:51:32 +0200, in comp.lang.c , "insin" : : >if (* -- q = malloc(sizeof * * q * 256)) : : whazzat * -- q ? You might need some parens there, to make it : unambiguous to the reader, plus its as easy to simply use array : notation surely? What do you mean by array notation? Could you clarify what way to rewrite the above to have it in the array notation you mention.
|
Wed, 02 Feb 2005 23:34:19 GMT |
|
 |
Kevin Easto #9 / 15
|
 dynamic array of pointers
Quote:
> On Sat, 17 Aug 2002 14:26:18 +0000 (UTC), in comp.lang.c , Kevin
>>> Hello- >>> i know it's possible to use an array of pointers i.e. char >>> *array[25]; i use this all the time. but, is it possible to make a >>> dynamic array of pointers i.e. use malloc to first make a pointer and >>> then malloc again to make an array of pointers? what would be the >>> syntax? i would appreciate any comments or references to K&R (i >>> couldn't find it in there but thats not to say its not there)... >>> thanks. >>> mark. :-) >>char **ptrarray = malloc(sizeof(char *) * n); > thats a slightly confusing way round to write it, at first glance I > read that as sizeof *n cast to a char*. > Also the gurus here would recommend using sizeof the object pointed to > rather than sizeof a type. > char ** ptrarray = malloc(n * sizeof *ptrarray);
Yeah, I considered that, but I thought in this instance it had more pedagogic value as sizeof(char *).
|
Wed, 02 Feb 2005 23:42:24 GMT |
|
 |
Mark McIntyr #10 / 15
|
 dynamic array of pointers
On Sat, 17 Aug 2002 17:34:19 +0200, in comp.lang.c , "insin" Quote:
>"Mark McIntyre" >: On Sat, 17 Aug 2002 16:51:32 +0200, in comp.lang.c , "insin" >: >: >if (* -- q = malloc(sizeof * * q * 256)) >: >: whazzat * -- q ? You might need some parens there, to make it >: unambiguous to the reader, plus its as easy to simply use array >: notation surely? >What do you mean by array notation?
array notation, you know, how you write arrays? Quote: >Could you clarify what way to rewrite the above to have it in the array notation you mention.
q = malloc(n * sizeof *q) for(int i=0;i<n;i++) q[i] = malloc(256 * sizeof **q); -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Wed, 02 Feb 2005 23:42:45 GMT |
|
 |
insi #11 / 15
|
 dynamic array of pointers
: >What do you mean by array notation? : array notation, you know, how you write arrays? : >Could you clarify what way to rewrite the above to have it in the array notation you mention. : q = malloc(n * sizeof *q) : for(int i=0;i<n;i++) : q[i] = malloc(256 * sizeof **q); I am surprised you are concerned about this non-issue, whereas the the real issue with the code I posted is not addressed by you at all (hint: undefined behavior).
|
Wed, 02 Feb 2005 23:49:30 GMT |
|
 |
Ben Pfaf #12 / 15
|
 dynamic array of pointers
Quote:
> char * * p = malloc(sizeof * p * 8); > if (p) { > char * * q = p + 8; > while (q != p) > if (* -- q = malloc(sizeof * q * 256))
Surely you mean `sizeof **q'? Quote: > * * q = '0', * q [1] = 0; > }
Your use of whitespace is...intriguing. The way you space things make it look like unary * is a binary operator. Here is the way I would write that: char **p = malloc(sizeof *p * 8); if (p != NULL) { int i; for (i = 0; i < 8; i++) if ((p[i] = malloc(sizeof **p * 256)) != NULL) strcpy (p[i], "0"); Quote: }
-- char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[] ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x1f6},*p= b,x,i=24;for(;p+=!*p;*p/=4)switch(x=*p&3)case 0:{return 0;for(p--;i--;i--)case 2:{i++;if(1)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
|
Thu, 03 Feb 2005 00:27:17 GMT |
|
 |
Mark McIntyr #13 / 15
|
 dynamic array of pointers
On Sat, 17 Aug 2002 17:49:30 +0200, in comp.lang.c , "insin" Quote:
>: >What do you mean by array notation? >: array notation, you know, how you write arrays? >: >Could you clarify what way to rewrite the above to have it in the array notation you mention. >: q = malloc(n * sizeof *q) >: for(int i=0;i<n;i++) >: q[i] = malloc(256 * sizeof **q); >I am surprised you are concerned about this non-issue, whereas the the real issue with the code I posted is not addressed by you at >all (hint: undefined behavior).
*shrug*. Your code was so hard to read, with all that whitespace and obfuscation, that I frankly didn't bother to read it right through. Others have undoubtedly commented on any UB. As for my "nonissue", IMHO gratuitous use of pointers when array notation will do the job is either someone trying to be too clever, or someone who hasn't realised the equivalence of the notation. -- Mark McIntyre CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html> CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
|
Thu, 03 Feb 2005 00:42:09 GMT |
|
 |
Richard B #14 / 15
|
 dynamic array of pointers
Quote:
> On Sat, 17 Aug 2002 16:51:32 +0200, in comp.lang.c , "insin"
> >if (* -- q = malloc(sizeof * * q * 256)) > whazzat * -- q ? You might need some parens there, to make it > unambiguous to the reader,
This _is_ unambiguous. It must group as *(--q) (it can hardly group as (*--)q, or as (*)--(q), what?), and this _must_ evaluate to the value pointed at by the pointer one less than the current q (and, as a side effect, decrease q to that pointer one less than its current value). There is no possible other interpretation. Richard
|
Fri, 04 Feb 2005 18:53:03 GMT |
|
 |
David Thompso #15 / 15
|
 dynamic array of pointers
... Quote: > : char * * p = malloc(sizeof * p * 8); > : if (p) { > : char * * q = p + 8; > : while (q != p) > : if (* -- q = malloc(sizeof * q * 256)) > sorry this should read > if (* -- q = malloc(sizeof * * q * 256)) > : * * q = '0', * q [1] = 0;
I hope you meant (*q)[1] = 0 /* or '\0' */ there. As written on the first iteration it writes to p[8] which you have no right to <G>, and if that UB happens to be (or seem) benign, on each subsequent iteration it clobbers the value carefully set by the previous one. Or you could replace _both_ character assignments by one strcpy(*q,"0"). PS- the bug you fixed didn't cause UB. sizeof(char) must be 1, and sizeof(char *) must be at least 1, so using sizeof *q may (and usually will) waste memory, but it will never allocate less than you apparently intended and presumably will (later) use. Quote: > : }
(That isn't a smiley, it's the quoted closing brace. <G>) -- - David.Thompson 1 now at worldnet.att.net
|
Mon, 07 Feb 2005 09:39:48 GMT |
|
|