By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

pointers to arrays of ints.

P: n/a
/*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>----------------*/
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Thu, 22 Apr 2004 03:06:26 +0100, "Hal Styli" <no_spam@all> wrote in
comp.lang.c:
/*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.

--
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++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2

P: n/a
On Thu, 22 Apr 2004 03:06:26 +0100, in comp.lang.c , "Hal Styli"
<no_spam@all> wrote:
/*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.
--
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>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #3

P: n/a
Jack Klein wrote:
On Thu, 22 Apr 2004 03:06:26 +0100, "Hal Styli" <no_spam@all> wrote in
comp.lang.c:
/*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.

--
++acr@,ka"
Nov 14 '05 #4

P: n/a
"Jack Klein" <ja*******@spamcop.net> wrote in message
news:tg********************************@4ax.com...
On Thu, 22 Apr 2004 03:06:26 +0100, "Hal Styli" <no_spam@all> wrote in
comp.lang.c:
<snip>
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
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.