473,388 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

pointers to arrays of ints.

/*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
4 1943
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
2
by: warnockg90 | last post by:
Hi. I am trying to write a program that utilises pointers to dynamically allocate memory. Initially I used arrays to run the program but this is not so good when you have user inputted values and...
8
by: macawm | last post by:
Hi all, What is the difference between these two statements? int* a = new int and int a The reason I ask is because this int len = sizeof(a) / sizeof(*a) works for the second construct, but...
14
by: code break | last post by:
what is the difference in this pointers decalarition ? int *ptr; and int (*ptr);
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.