pointers to arrays of ints.

H

Hal Styli

/*hello could someone please help me with the
errors i get related to pointers to arrays of ints.

I want to declare something like
int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
rather than
int d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
It seems to work in the code below but I get compile
warnings and Im concerned about portability.

thanks in advance Hal. */
/*------<start of code>------------------*/
#include <stdio.h>

#define entries(x) ( sizeof(x) / sizeof(x[0]) )
#define max entries(a)

#define pr(x) printf( "%s=%3d ", #x, x );
#define prln(x) printf( "%s=%3d\n", #x, x );

int main()
{
int *p,i;
int x[] = { 23,45,46,77,18,69,37,12,63,74 };
int *b = (int *) &x;

/* Now try and declare without intermediate variable.
* My compiler gives (8) nonportable pointer conversion
* warnings...*/
int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };

/*---------------------------------------------*/
putchar('\n'); pr(entries(x)); putchar('\n');

for( i=0,p=x; i<entries(x) ; i++ , p++ )
pr(*p);

putchar('\n');
/*---------------------------------------------*/
putchar('\n'); pr(entries(b)); putchar('\n');

for( i=0,p=b; i<entries(b) ; i++ , p++ )
pr(*p);

putchar('\n');
/*---------------------------------------------*/
putchar('\n'); pr(entries(d)); putchar('\n');

/* compiler complains about the p=d in this
* for() statement: Suspicious pointer conversion*/
for( i=0, p=d; i<entries(d) ; i++ , p++ )
pr(*p);
/*---------------------------------------------*/
return 0;
}
/*------<end of code>-<start of output>--------*/
entries(x)= 10
*p= 23 *p= 45 *p= 46 *p= 77 *p= 18 *p= 69 *p= 37 *p= 12 *p= 63 *p= 74

entries(b)= 1
*p= 23

entries(d)= 8
*p= 7 *p= 11 *p= 18 *p= 54 *p= 64 *p= 55 *p= 37 *p= 38
/*-----------------<end of output>----------------*/
 
J

Jack Klein

/*hello could someone please help me with the
errors i get related to pointers to arrays of ints.

I want to declare something like
int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
rather than
int d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
It seems to work in the code below but I get compile
warnings and Im concerned about portability.

thanks in advance Hal. */
/*------<start of code>------------------*/
#include <stdio.h>

#define entries(x) ( sizeof(x) / sizeof(x[0]) )
#define max entries(a)

#define pr(x) printf( "%s=%3d ", #x, x );
#define prln(x) printf( "%s=%3d\n", #x, x );

int main()
{
int *p,i;
int x[] = { 23,45,46,77,18,69,37,12,63,74 };
int *b = (int *) &x;

The line above is much more readable, and perfectly correct, written
as:

int *b = x;
/* Now try and declare without intermediate variable.
* My compiler gives (8) nonportable pointer conversion
* warnings...*/
int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };

If you want an array of pointers to int, you have to put pointers to
int in it. You are putting ints in it. You need to do something
like:

int c[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
int *d[] = { c, c+1, c+2, /* and so on */ };
/*---------------------------------------------*/
putchar('\n'); pr(entries(x)); putchar('\n');

for( i=0,p=x; i<entries(x) ; i++ , p++ )
pr(*p);

putchar('\n');
/*---------------------------------------------*/
putchar('\n'); pr(entries(b)); putchar('\n');

for( i=0,p=b; i<entries(b) ; i++ , p++ )
pr(*p);

putchar('\n');
/*---------------------------------------------*/
putchar('\n'); pr(entries(d)); putchar('\n');

/* compiler complains about the p=d in this
* for() statement: Suspicious pointer conversion*/
for( i=0, p=d; i<entries(d) ; i++ , p++ )
pr(*p);

....as it should. The name of an array in this context it converted to
a pointer to its first element. But the type of d is not "array of
ints", which gets converted to "pointer to int". The type of d is
"array of pointers to int", which gets converted to "pointer to
pointer to int", which does not match the type of p.
/*---------------------------------------------*/
return 0;
}
/*------<end of code>-<start of output>--------*/
entries(x)= 10
*p= 23 *p= 45 *p= 46 *p= 77 *p= 18 *p= 69 *p= 37 *p= 12 *p= 63 *p= 74

entries(b)= 1
*p= 23

entries(d)= 8
*p= 7 *p= 11 *p= 18 *p= 54 *p= 64 *p= 55 *p= 37 *p= 38
/*-----------------<end of output>----------------*/

Why do you think you want an array of pointers to int? Tell us the
problem you are trying to solve, and we can show you the proper way to
solve it.
 
M

Mark McIntyre

/*hello could someone please help me with the
errors i get related to pointers to arrays of ints.
I want to declare something like
int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };

thats an array of pointers to ints, but you initialised with it ints by
mistake and....
* My compiler gives (8) nonportable pointer conversion
* warnings...*/

indeed it should give a warning. An array of pointers can't safely be
initialised with a set of integers, the two are diffrerent..
int main()
{
int *p,i;
int x[] = { 23,45,46,77,18,69,37,12,63,74 };
int *b = (int *) &x;

b is a pointer to an int.
&x is an pointer to an array of ints.
you can't initialse a pointer to an int with a pointer to an array.
The cast is hiding a compiler warning which is presumably why you put it
in.
 
S

Sam Dennis

Jack said:
/*hello could someone please help me with the
errors i get related to pointers to arrays of ints.

int *d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };

If you want an a pointer to an array of ints, try this:

int d[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
int (*dp)[sizeof d / sizeof *d] = &d;
If you want an array of pointers to int, you have to put pointers to
int in it.

int c[] = { 7, 11, 18, 54, 64, 55, 37, 38, };
int *d[] = { c, c+1, c+2, /* and so on */ };

That's rather tedious; something like this should work:

int d[][1] = { 7, 11, 18, 54, 64, 55, 37, 38, };

Obviously, it's not actually an array of pointers to int, but its
elements can normally be treated as such.
 
H

Hal Styli

Jack Klein said:
comp.lang.c:

said:
int x[] = { 23,45,46,77,18,69,37,12,63,74 };
int *b = (int *) &x;

The line above is much more readable, and perfectly correct, written
as:

int *b = x;

Fair enough, so how can I combine the 2 lines into one,
i.e. have a pointer to an array of ints, effectively:-

'point b to this: { 23,45,46,77,18,69,37,12,63,74 } '

I hope this doesnt confuse matters but I guess I want to emulate

if( (b=malloc(BLEN*sizeof(int)))==NULL )
{
fputs("\n\nmalloc problem\n\n",stderr);
exit(9);
}

/* fill the array in some way */
for( i=0,p=b; i<BLEN; i++,p++ )
*p=rand()%100;

in one line.

There is not specific application in mind here, I'm just trying to get my
head around pointers.

Thanks again.
Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top