473,395 Members | 1,622 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,395 software developers and data experts.

Initializing Pointer to an array

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.

Nov 15 '05 #1
21 3061
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

Nov 15 '05 #2
ru********@rediffmail.com 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.
Nov 15 '05 #3
Christopher Benson-Manica wrote:

<snipped rest of code>
ru********@rediffmail.com 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++)
??

Nov 15 '05 #4
Antonio Contreras <an*****@gmail.com> 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.
Nov 15 '05 #5
Christopher Benson-Manica wrote:
Antonio Contreras <an*****@gmail.com> 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.
Nov 15 '05 #6
Flash Gordon wrote:
Christopher Benson-Manica wrote:
Antonio Contreras <an*****@gmail.com> 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.

Nov 15 '05 #7
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

Nov 15 '05 #8

In article <11**********************@g44g2000cwa.googlegroups .com>, "Antonio Contreras" <an*****@gmail.com> writes:
Christopher Benson-Manica wrote:
ru********@rediffmail.com 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 mi************@microfocus.com

Global warming is just a theory. This is Intelligent Defrosting. -- "Gregg"
Nov 15 '05 #9
On 20 Oct 2005 15:36:14 -0700, "Antonio Contreras" <an*****@gmail.com>
wrote in comp.lang.c:
Flash Gordon wrote:
Christopher Benson-Manica wrote:
Antonio Contreras <an*****@gmail.com> 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
Nov 15 '05 #10
Peter Nilsson <ai***@acay.com.au> wrote:
It's not mandatory un C90, just desirable.


I suppose that's strictly correct, but it's probably safe to assume
that a reasonable host environment will attempt to use the termination
status of main. I would think that the chances of UB on a hosted
implentation would be rather high.

--
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.
Nov 15 '05 #11
On Fri, 21 Oct 2005 14:34:06 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Peter Nilsson <ai***@acay.com.au> wrote:
It's not mandatory un C90, just desirable.


I suppose that's strictly correct, but it's probably safe to assume
that a reasonable host environment will attempt to use the termination
status of main. I would think that the chances of UB on a hosted
implentation would be rather high.


C *and* C++ specify that reaching the the terminating } of main() will
return 0; so this function *always* return a valid value.
Nov 15 '05 #12
Zara <no***********@terra.es> wrote:
C *and* C++ specify that reaching the the terminating } of main() will
return 0; so this function *always* return a valid value.


C89 does not.

--
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.
Nov 15 '05 #13
Christopher Benson-Manica wrote:
Zara <no***********@terra.es> wrote:
C *and* C++ specify that reaching the the terminating } of main() will
return 0; so this function *always* return a valid value.


C89 does not.


And, just to emphasise the point, there are far more people using
implementations that can support C89 correctly (modulo bugs) than there
are people using conforming C99 implementations. For a start, neither
gcc nor MS VC++ fully support C99 but both fully support C89 (modulo bugs).
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #14
Jack Klein <ja*******@spamcop.net> writes:
Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b,


Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */
}

Personally, I'd be in favor of adding a rule that makes the two
forms equivalent in the case of a 0-valued constant expression.
But that's another story.

Nov 15 '05 #15
Jack Klein wrote:
On 20 Oct 2005 15:36:14 -0700, "Antonio Contreras" <an*****@gmail.com>
wrote in comp.lang.c:
Flash Gordon wrote:
Christopher Benson-Manica wrote:
> Antonio Contreras <an*****@gmail.com> 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.


I didn't imply that. What I tried to say is that it "looks" like it,
and that it is confusing when you're learning C. IMHO, given that b is
an array, b[0] is less confusing than *b.
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.


I acknowledge that much. I literally said "I prefer the _totally
equivalent_..."
[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.


Again, I didn't try to imply that. I guess it was a really bad wording
from my side.

Nov 15 '05 #16
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote:
Jack Klein <ja*******@spamcop.net> writes:
Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b,
Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */


Are you sure? n1124 states in 6.5.2.1(2) that

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So b[0] is the same as *(b+0) which is the same as *b. Both
expressions should evaluate to the address of a with type array of 10
int which, since it is not one of the exceptions, will then be
converted to the address of a[0] with type pointer to int.
}

Personally, I'd be in favor of adding a rule that makes the two
forms equivalent in the case of a 0-valued constant expression.
But that's another story.

<<Remove the del for email>>
Nov 15 '05 #17
Barry Schwarz <sc******@deloz.net> wrote:
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote:
Jack Klein <ja*******@spamcop.net> writes:
Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b,
Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */


Are you sure? n1124 states in 6.5.2.1(2) that

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So b[0] is the same as *(b+0) which is the same as *b.


They are not the same. `b+0' has an additional constraint:
6.5.6#2
# [#2] For addition, either both operands shall have
# arithmetic type, or one operand shall be a pointer to an
# object type and the other shall have integer type.

("Object type" is a complete type.)
Both
expressions should evaluate to the address of a with type array of 10
int which,
`*b' is an incomplete (array) type lvalue.
since it is not one of the exceptions, will then be
converted to the address of a[0] with type pointer to int.


Yes.

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #18
S.Tobias wrote:
Barry Schwarz <sc******@deloz.net> wrote:
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote:
Jack Klein <ja*******@spamcop.net> writes:
Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b,

Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */


Are you sure? n1124 states in 6.5.2.1(2) that

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So b[0] is the same as *(b+0) which is the same as *b.

They are not the same. `b+0' has an additional constraint:
6.5.6#2
# [#2] For addition, either both operands shall have
# arithmetic type, or one operand shall be a pointer to an
# object type and the other shall have integer type.

("Object type" is a complete type.)


Yes.
Both
expressions should evaluate to the address of a with type array of 10
int which,


`*b' is an incomplete (array) type lvalue.


*b being an incomplete type is irrelevant since it is *(b+0) NOT *b +0
so 0 is added to b not to *b.
since it is not one of the exceptions, will then be
converted to the address of a[0] with type pointer to int.


Yes.

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #19
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
S.Tobias wrote:
Barry Schwarz <sc******@deloz.net> wrote:
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote: void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */
[snip]So b[0] is the same as *(b+0) which is the same as *b. ....Both
expressions should evaluate to the address of a with type array of 10
int which,


`*b' is an incomplete (array) type lvalue.


*b being an incomplete type is irrelevant since it is *(b+0) NOT *b +0
so 0 is added to b not to *b.

Right. I did mean `*b', but I misunderstood what Barry had said, so my
answer was irrelevant, too.

One more try: `*b' evaluates to an incomplete array type lvalue
for the array `a', which is immediately converted to the pointer to
the first element of `a' (not: "to the address of `a'"; unless "address"
is understood as "location").

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 15 '05 #20
Christopher Benson-Manica wrote:
Peter Nilsson <ai***@acay.com.au> wrote: [
Unless you are using a C99 compiler, you must return a value from
main().
] It's not mandatory un C90, just desirable.


I suppose that's strictly correct, but it's probably safe to assume
that a reasonable host environment will attempt to use the termination
status of main.


It's not safe to assume that, but it is something to consider.
I would think that the chances of UB on a hosted
implentation would be rather high.


The standard doesn't define what the host does with an undefined
status.
But it doesn't define what the host does with a successful status
either.

Hence, if one is 'UB', so is the other!

Pragmatically though, programmers who leave out the return value from
main under C89 are risking future problems in cases where their program
is used in higher level script (e.g. shell script). Such scripting
languages may terminate on spurious error signals from the program.

So 'UB' in this case is probably better phrased as undesirable
behaviour,
rather than undefined.

--
Peter

Nov 15 '05 #21
Barry Schwarz <sc******@deloz.net> writes:
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <tx*@alumnus.caltech.edu>
wrote:
Jack Klein <ja*******@spamcop.net> writes:
Plus the C language absolutely requires that the expression b[0] be
evaluated as if written *b,


Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */


Are you sure? n1124 states in 6.5.2.1(2) that

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So b[0] is the same as *(b+0) which is the same as *b. Both
expressions should evaluate to the address of a with type array of 10
int which, since it is not one of the exceptions, will then be
converted to the address of a[0] with type pointer to int.


I gather this has been said already, but briefly:

The equivalence holds, but a requirement for the + operator
is that a pointer operand be a pointer to an object type
(ie, rather than a pointer to an incomplete type). The
variable 'b' is a pointer to an incomplete type, and so
there is a constraint violation.
Nov 15 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: masood.iqbal | last post by:
I am having lots of trouble getting a simple program that initializs a dynamically allocated 2D array to work. My 2D array is not getting initialized properly, and additionally I am getting a...
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
7
by: nk | last post by:
Hi, I'm a newbie on this language. I would be very happy if you help me about the following issue: The code below, reads some names(strings), stores them, and stores the addresses in the pointer...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.