Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initializing Pointer to an array

Reply
Thread Tools

Initializing Pointer to an array

 
 
rupesh_533@rediffmail.com
Guest
Posts: n/a
 
      10-20-2005
I am assuming the following things.
1.Pointer to an integer means it points to an integer,On incrementing
the pointer,it points to the next integer in memory.
2.Pointer to an array of some size means it points to an array,On
incrementing the pointer,it should point to next array of that size.

Correct me if i am wrong.
We declare a pointer to an integer array of Size N as
int (*a)[N];

Now My Problem is
I have an array of 100 Integers,Pointer to an array of 10 integers
int (*a)[10];
int b[100];
How can i initialize the pointer a to point to the array b base
address.

 
Reply With Quote
 
 
 
 
rupesh_533@rediffmail.com
Guest
Posts: n/a
 
      10-20-2005
I am able to initialize the pointer to the array as
int (*a)[10];
int b[100];

a = &b[0];
But i am getting the warning
warning: assignment from incompatible pointer type

My Code is
int main()
{
int (*a)[10];
int b[100];
int i;

for(i=0;i<100;i++)
b[i]=i;

a = &b[0];

printf(" %u %u %d %d\n",a ,&b[0],**a,b[0]);
a++;
printf(" %u %u %d %d\n",a ,&b[10],**a,b[10]);
a++;
printf(" %u %u %d %d\n",a , &b[20],**a,b[20]);
a++;
printf(" %u %u %d %d\n",a , &b[30],**a,b[30]);
}

I am getting correct results as
3221219184 3221219184 0 0
3221219224 3221219224 10 10
3221219264 3221219264 20 20
3221219304 3221219304 30 30

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-20-2005
wrote:

> I am able to initialize the pointer to the array as
> int (*a)[10];
> int b[100];


> a = &b[0];
> But i am getting the warning
> warning: assignment from incompatible pointer type


Of course you are. a is a pointer to an array of 10 ints (which
you probably do not need), while &b[0] is a pointer to an int. Your
compiler is doing you a favor.

> My Code is
> int main()


int main( void ) /* better */

> {
> int (*a)[10];
> int b[100];
> int i;


> for(i=0;i<100;i++)


for( i=0; i < sizeof b; i++) /* better */

> b[i]=i;


> a = &b[0];


Wrong, as I said.

> printf(" %u %u %d %d\n",a ,&b[0],**a,b[0]);


Wrong in multiple ways:

1) You did not include <stdio.h>.
2) %u is not the conversion specifier you want for pointers.

printf( " %p %p %d %d\n", (void*)a, (void*)&b[0], **a, b[0] );

Note the casts; they are required.

> a++;
> printf(" %u %u %d %d\n",a ,&b[10],**a,b[10]);
> a++;
> printf(" %u %u %d %d\n",a , &b[20],**a,b[20]);
> a++;
> printf(" %u %u %d %d\n",a , &b[30],**a,b[30]);


Unless you are using a C99 compiler, you must return a value from
main(). Your compiler should have warned you about this unfortunate
omission.

> }


--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Antonio Contreras
Guest
Posts: n/a
 
      10-20-2005
Christopher Benson-Manica wrote:

<snipped rest of code>

> wrote:
> > int main()

>
> int main( void ) /* better */
>
> > {
> > int (*a)[10];
> > int b[100];
> > int i;

>
> > for(i=0;i<100;i++)

>
> for( i=0; i < sizeof b; i++) /* better */
>


Shouldn't that be:
for (i = 0; i < ((sizeof b) / (sizeof int)); i++)
??

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-20-2005
Antonio Contreras <> wrote:

> for (i = 0; i < ((sizeof b) / (sizeof int)); i++)


Yes. Ouch. (And thank you.)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-20-2005
Christopher Benson-Manica wrote:
> Antonio Contreras <> wrote:
>
>>for (i = 0; i < ((sizeof b) / (sizeof int)); i++)

>
> Yes. Ouch. (And thank you.)


Or better yet:
for (i = 0; i < ((sizeof b) / (sizeof *b)); i++)

Then it will compile (apart from being incomplete and the certainty of
me introducing an error in my correction) and be independant of the type
of array b.


--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Antonio Contreras
Guest
Posts: n/a
 
      10-20-2005
Flash Gordon wrote:
> Christopher Benson-Manica wrote:
> > Antonio Contreras <> wrote:
> >
> >>for (i = 0; i < ((sizeof b) / (sizeof int)); i++)

> >
> > Yes. Ouch. (And thank you.)

>
> Or better yet:
> for (i = 0; i < ((sizeof b) / (sizeof *b)); i++)
>
> Then it will compile (apart from being incomplete and the certainty of
> me introducing an error in my correction) and be independant of the type
> of array b.
>
>


Well, for purely aesthetic reasons I would prefer the totally
equivalent:

for (i = 0; i < ((sizeof b) / (sizeof b[0])); i++)

Call me paranoid, but the equivalence between arrays and pointers is
confusing enough [1] without dereferencing pointers that have been
created by the decay of an array name.

[1] Actually I've gotten used to it and it's not confusing anymore, but
it was really confusing two years ago.

 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      10-20-2005

In article < .com>, "Antonio Contreras" <> writes:
> Christopher Benson-Manica wrote:
> > wrote:
> > > int (*a)[10];
> > > int b[100];
> > > int i;

> >
> > > for(i=0;i<100;i++)

> >
> > for( i=0; i < sizeof b; i++) /* better */

>
> Shouldn't that be:
> for (i = 0; i < ((sizeof b) / (sizeof int)); i++)


When the sizeof operator is applied to a type, the name of the type
must be parenthesized. You're missing parentheses around "int"
(which is ironic, considering all the unnecessary sets of parentheses
you have there).

What would be better is

for (i = 0; i < sizeof b / sizeof *b; i++)

which works for a "b" of any (complete) array type.

--
Michael Wojcik

Global warming is just a theory. This is Intelligent Defrosting. -- "Gregg"
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      10-20-2005
Christopher Benson-Manica wrote:
>
> Unless you are using a C99 compiler, you must return a value from
> main().


It's not mandatory un C90, just desirable.

> Your compiler should have warned you about this unfortunate
> omission.


Perhaps, but unlike C++, both C90 and C99 allow non-void functions to
fail
to return a value. So long as the calling function doesn't attempt to
use
the value, all is fine.

As this is a <cough> feature of the language, many old compilers won't
issue a warning for this.

--
Peter

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-21-2005
On 20 Oct 2005 15:36:14 -0700, "Antonio Contreras" <>
wrote in comp.lang.c:

> Flash Gordon wrote:
> > Christopher Benson-Manica wrote:
> > > Antonio Contreras <> wrote:
> > >
> > >>for (i = 0; i < ((sizeof b) / (sizeof int)); i++)
> > >
> > > Yes. Ouch. (And thank you.)

> >
> > Or better yet:
> > for (i = 0; i < ((sizeof b) / (sizeof *b)); i++)
> >
> > Then it will compile (apart from being incomplete and the certainty of
> > me introducing an error in my correction) and be independant of the type
> > of array b.
> >
> >

>
> Well, for purely aesthetic reasons I would prefer the totally
> equivalent:
>
> for (i = 0; i < ((sizeof b) / (sizeof b[0])); i++)
>
> Call me paranoid, but the equivalence between arrays and pointers is
> confusing enough [1] without dereferencing pointers that have been
> created by the decay of an array name.


The sizeof operator never evaluates the value of its operand in C
prior to C99, and only to determine the size of a variable length
array in C99. Under no circumstances does sizeof dereference a
pointer given to it as an argument.

Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b, so the Christopher's code and your
alternative are exactly equivalent in C.

> [1] Actually I've gotten used to it and it's not confusing anymore, but
> it was really confusing two years ago.


In either case, there is no evaluation of the value of b[0] or *b, and
no dereference.

--
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
initializing array using pointer arun C Programming 31 12-15-2005 09:42 AM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57