assign address to pointer array
Author |
Message |
Peter Humphre #1 / 10
|
 assign address to pointer array
I want to assign the address of a thing to an array of (addresses of things), along the lines of: #include <malloc.h> int main(){ int *x; int y=64; x = (int*)malloc((size_t)3*sizeof(int)); x[1] = &y; return 0; Quote: }
but, to my understanding, x[1] is equivalent to *(x+1). So I need to something like (x+1) = &y, but (x+1) isn't an l-value. Obviously doing something stupid... Thanks
|
Sun, 07 Dec 2003 11:57:14 GMT |
|
 |
Jack Klei #2 / 10
|
 assign address to pointer array
On Wed, 20 Jun 2001 03:57:14 GMT, "Peter Humphrey"
Quote: > I want to assign the address of a thing to an array of (addresses of > things), along the lines of: > #include <malloc.h>
There is no header named malloc.h in C. malloc() is prototyped in <stdlib.h> for 12 years now. Quote: > int main(){ > int *x; > int y=64; > x = (int*)malloc((size_t)3*sizeof(int));
Don't cast the return value of malloc() in C. The language does not require it and it can hide errors. The cast to size_t is totally unnecessary for several reasons. The first is that the prototype tells the compiler that the parameter is a size_t so it will perform the conversion automatically. The second is that the sizeof operator always yields a size_t value anyway. The best way to call malloc() (after including the proper <stdilb.h>) is: x = malloc(3 * sizeof *x); Quote: > x[1] = &y; > return 0; > } > but, to my understanding, x[1] is equivalent to *(x+1). So I need to > something like (x+1) = &y, but (x+1) isn't an l-value. > Obviously doing something stupid... > Thanks
Why do you think that x[1] = &y; does not do what you want? Once you have performed this assignment, *x[1] will be the value stored in y. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Sun, 07 Dec 2003 12:13:03 GMT |
|
 |
Peter Humphre #3 / 10
|
 assign address to pointer array
Jack Thanks very much for reply. I'm mixing up c and c++ code. Sorry for confusing the issue. If I now compile the following code: #include <stdlib.h> int main(void){ int *x; int y=64, z; x = malloc(3*sizeof(int*)); x[1] = &y; /* line 6 */ z=*x[1]; /* line 7 */ return 0; Quote: }
line 6 "int ' differs in levels of indirection from 'int *'" line 7 "illegal indirection" Thans again
Quote: > On Wed, 20 Jun 2001 03:57:14 GMT, "Peter Humphrey"
> > I want to assign the address of a thing to an array of (addresses of > > things), along the lines of: > > #include <malloc.h> > There is no header named malloc.h in C. malloc() is prototyped in > <stdlib.h> for 12 years now. > > int main(){ > > int *x; > > int y=64; > > x = (int*)malloc((size_t)3*sizeof(int)); > Don't cast the return value of malloc() in C. The language does not > require it and it can hide errors. The cast to size_t is totally > unnecessary for several reasons. The first is that the prototype > tells the compiler that the parameter is a size_t so it will perform > the conversion automatically. The second is that the sizeof operator > always yields a size_t value anyway. > The best way to call malloc() (after including the proper <stdilb.h>) > is: > x = malloc(3 * sizeof *x); > > x[1] = &y; > > return 0; > > } > > but, to my understanding, x[1] is equivalent to *(x+1). So I need to > > something like (x+1) = &y, but (x+1) isn't an l-value. > > Obviously doing something stupid... > > Thanks > Why do you think that x[1] = &y; does not do what you want? Once you > have performed this assignment, *x[1] will be the value stored in y. > -- > Jack Klein > Home: http://JK-Technology.Com > FAQs for > comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html > comp.lang.c++ http://www.parashift.com/c++-faq-lite/ > alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Sun, 07 Dec 2003 12:43:17 GMT |
|
 |
Jack Klei #4 / 10
|
 assign address to pointer array
On Wed, 20 Jun 2001 04:43:17 GMT, "Peter Humphrey"
Quote: > Jack > Thanks very much for reply. I'm mixing up c and c++ code. Sorry for > confusing the issue.
That's OK, but there is no malloc.h header in C++ either. In C++ malloc() is prototyped in <stdlib.h> or in newer ISO compilers <cstdlib> could be included instead. In either case you do need the cast in C++. Quote: > If I now compile the following code:
Sorry, my mistake. Must be time to get some sleep. Let's try again... Quote: > #include <stdlib.h> > int main(void){ > int *x;
This should be: int **x; ...because x will point to an array of pointers to int. Therefore it must be a pointer to pointer. Quote: > int y=64, z; > x = malloc(3*sizeof(int*));
This is still better written as: x = malloc(3 * sizeof *x); That way if you later decide you want to store pointers to double instead of pointers to int, you only have to change the definition of x from int **x to double **x, you don't have to hunt down and change all the memory allocation calls. Quote: > x[1] = &y; /* line 6 */ > z=*x[1]; /* line 7 */ > return 0; > } > line 6 "int ' differs in levels of indirection from 'int *'" > line 7 "illegal indirection" > Thans again
-- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
Sun, 07 Dec 2003 12:53:33 GMT |
|
 |
#5 / 10
|
 assign address to pointer array
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Peter Humphre #6 / 10
|
 assign address to pointer array
that's what I was missing. Thanks very much for your help....much appreciated.
|
Sun, 07 Dec 2003 13:22:57 GMT |
|
 |
#7 / 10
|
 assign address to pointer array
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
Arthur H. Gol #8 / 10
|
 assign address to pointer array
Quote:
> Jack > Thanks very much for reply. I'm mixing up c and c++ code. Sorry for > confusing the issue. > If I now compile the following code: > #include <stdlib.h> > int main(void){ > int *x;
ITYM int **x; It's supposed to be a pointer to a bunch of int *'s, not a bunch of ints. Quote: > int y=64, z; > x = malloc(3*sizeof(int*));
You should always check the return value of malloc() [but you know that, of course ;-)] Quote: > x[1] = &y; /* line 6 */ > z=*x[1]; /* line 7 */ > return 0; > } > line 6 "int ' differs in levels of indirection from 'int *'" > line 7 "illegal indirection"
Be aware that the expression x[i] is equivalent to the expression *(x + i), so in order to store an int *, x must be of type int **, for example.
HTH, --ag Quote:
> > On Wed, 20 Jun 2001 03:57:14 GMT, "Peter Humphrey"
> > > I want to assign the address of a thing to an array of (addresses of > > > things), along the lines of: > > > #include <malloc.h> > > There is no header named malloc.h in C. malloc() is prototyped in > > <stdlib.h> for 12 years now. > > > int main(){ > > > int *x; > > > int y=64; > > > x = (int*)malloc((size_t)3*sizeof(int)); > > Don't cast the return value of malloc() in C. The language does not > > require it and it can hide errors. The cast to size_t is totally > > unnecessary for several reasons. The first is that the prototype > > tells the compiler that the parameter is a size_t so it will perform > > the conversion automatically. The second is that the sizeof operator > > always yields a size_t value anyway. > > The best way to call malloc() (after including the proper <stdilb.h>) > > is: > > x = malloc(3 * sizeof *x); > > > x[1] = &y; > > > return 0; > > > } > > > but, to my understanding, x[1] is equivalent to *(x+1). So I need to > > > something like (x+1) = &y, but (x+1) isn't an l-value. > > > Obviously doing something stupid... > > > Thanks > > Why do you think that x[1] = &y; does not do what you want? Once you > > have performed this assignment, *x[1] will be the value stored in y.
-- Artie Gold, Austin, TX (finger the cs.utexas.edu account for more info)
-- I am looking for work. Contact me.
|
Sun, 07 Dec 2003 12:58:19 GMT |
|
 |
Matt Gessne #9 / 10
|
 assign address to pointer array
Quote:
> I want to assign the address of a thing to an array of (addresses of > things), along the lines of: > #include <malloc.h> > int main(){ > int *x; > int y=64; > x = (int*)malloc((size_t)3*sizeof(int)); > x[1] = &y; > return 0; > } > but, to my understanding, x[1] is equivalent to *(x+1). So I need to > something like (x+1) = &y, but (x+1) isn't an l-value. > Obviously doing something stupid... > Thanks
If you're going to do that, you'd need *(x+1) And don't typecast malloc... Leave it be void * as it's declared. HTH
|
Sat, 13 Dec 2003 23:21:30 GMT |
|
 |
Martin Ambuh #10 / 10
|
 assign address to pointer array
Quote:
> I want to assign the address of a thing to an array of (addresses of > things), along the lines of:
For your consideration: #if 0 /* mha - The OP used the following, an inclusion of a non-standard header. A correction follows. */ #include <malloc.h> #endif #include <stdlib.h> /* mha */ int main() { #if 0 /* mha - The OP used the following, a declaration of a pointer-to-int, yet claimed he wanted "an array of (addresses of things)." A correction follows. */ int *x; #endif int **x; /* mha */ int y = 64; #if 0 /* mha - The OP used the following clutter. A replacement follows, retaining the OP's magic number of 3, however */ x = (int *)malloc((size_t) 3 * sizeof(int)); #endif x = malloc(3 * sizeof *x); /* mha */ /* mha - The OP did not check for success of malloc(). A simple-minded check is added below. */ if (!x) exit(EXIT_FAILURE); /* mha */ x[1] = &y; /* mha - The OP did not free the allocated memory. Added below. */ free(x); /* mha */ return 0; Quote: }
|
Sun, 14 Dec 2003 01:09:40 GMT |
|
|
|