Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer to an int array

Reply
Thread Tools

pointer to an int array

 
 
Neo
Guest
Posts: n/a
 
      01-03-2005
Hi Folks,


#include<stdio.h>
int main()
{
int (*p)[10];
int arr[10];
int i;

p = arr; /* <-- compiler warning */
for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
*((*p) + i) = i;

printf("%d\n", p[0][i]);
}

return 0;
}

In the above program compiler is giving following warning

$gcc pointer2array.c
pointer2array.c: In function `main':
pointer2array.c:8: warning: assignment from incompatible pointer type

Why the warning?

O'kay and what is the use of int (*p)[10] type of declaration ?
Does the compiler perform any check on the array bounds?
lets say if i declare int arr[12]...
-Neo


 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-03-2005
On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <>
wrote in comp.lang.c:

> Hi Folks,
>
>
> #include<stdio.h>
> int main()
> {
> int (*p)[10];
> int arr[10];
> int i;
>
> p = arr; /* <-- compiler warning */


The name of an array in an expression, other than when it is the
operand of the 'sizeof' or '&' operators, is converted to a pointer to
its first element. So in the statement above, you are assigning the
address of an int (arr[0]) to a pointer to an array. But arr[0] is an
int, not an array of ints.

Replace with:

p = &arr;

> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
> *((*p) + i) = i;
>
> printf("%d\n", p[0][i]);
> }
>
> return 0;
> }
>
> In the above program compiler is giving following warning
>
> $gcc pointer2array.c
> pointer2array.c: In function `main':
> pointer2array.c:8: warning: assignment from incompatible pointer type
>
> Why the warning?
>
> O'kay and what is the use of int (*p)[10] type of declaration ?
> Does the compiler perform any check on the array bounds?
> lets say if i declare int arr[12]...


--
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
 
 
 
 
Neo
Guest
Posts: n/a
 
      01-03-2005

"Jack Klein" <> wrote in message
news:...
> On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <>
> wrote in comp.lang.c:
>
>> Hi Folks,
>>
>>
>> #include<stdio.h>
>> int main()
>> {
>> int (*p)[10];
>> int arr[10];
>> int i;
>>
>> p = arr; /* <-- compiler warning */

>
> The name of an array in an expression, other than when it is the
> operand of the 'sizeof' or '&' operators, is converted to a pointer to
> its first element. So in the statement above, you are assigning the
> address of an int (arr[0]) to a pointer to an array. But arr[0] is an
> int, not an array of ints.
>
> Replace with:
>
> p = &arr;
>


O'kay, dats fine.
if I change the declaration above as :

int (*p)[]; <-- pointer2array.c:12: error: invalid use of array with
unspecified bounds
int arr[10];
p = &arr;
for(i=0; i <= 12; i++){
*((*p) + i) = i;
printf("%d\n", p[0][i]);
}

compiler error!
what's the use of array index in the above declaration?
as i can go upto 12 or more...

-Neo



>> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
>> *((*p) + i) = i;
>>
>> printf("%d\n", p[0][i]);
>> }
>>
>> return 0;
>> }
>>
>> In the above program compiler is giving following warning
>>
>> $gcc pointer2array.c
>> pointer2array.c: In function `main':
>> pointer2array.c:8: warning: assignment from incompatible pointer type
>>
>> Why the warning?
>>
>> O'kay and what is the use of int (*p)[10] type of declaration ?
>> Does the compiler perform any check on the array bounds?
>> lets say if i declare int arr[12]...

>
> --
> 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
 
Mark McIntyre
Guest
Posts: n/a
 
      01-03-2005
On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
<> wrote:

>if I change the declaration above as :
>
> int (*p)[]; <-- pointer2array.c:12: error: invalid use of array with unspecified bounds
>
>compiler error!


yes, its an error

>what's the use of array index in the above declaration?


none - its an illegal construct

>as i can go upto 12 or more...


only by invoking undefined behaviour....

Don't do that.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
gooch
Guest
Posts: n/a
 
      01-03-2005
You can index up to whatever value you want. p[1000] is fine. The
result is going to be undefined. In most cases you would get a seg
fault but that may not be the case. You have to do the bounds checking
yourself.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      01-03-2005
"gooch" <> wrote:

[ Using Google-Broken-Beta is not an excuse not to show any context.
Learn to use it to quote properly or get a real newsreader. Context
reinstated. ]

> "Neo" <> wrote:
>
> > if I change the declaration above as :
> >
> > int (*p)[]; <-- pointer2array.c:12: error: invalid use of array with
> > unspecified bounds
> > int arr[10];
> > p = &arr;
> > for(i=0; i <= 12; i++){
> > *((*p) + i) = i;
> > printf("%d\n", p[0][i]);
> > }
> >
> > compiler error!
> > what's the use of array index in the above declaration?
> > as i can go upto 12 or more...


> You can index up to whatever value you want. p[1000] is fine. The
> result is going to be undefined.


I don't see how you can write those sentences together. The result of
p[10] and over are indeed going to be undefined, which is exactly why
such indices are _not_ fine.

> In most cases you would get a seg fault but that may not be the case.


If you're lucky, you'll get a segfault. If you're unlucky, you won't see
the segfault, but your customer will. If you're _really_ unlucky,
neither of you will get a segfault, but your program will slowly
scribble over all your customer's data, and he'll sue you. Don't run
over the end of your arrays, not even when you think "it'll be safe,
because you'll get a segfault anyway".

Richard
 
Reply With Quote
 
gooch
Guest
Posts: n/a
 
      01-03-2005
"I don't see how you can write those sentences together. ....."

I am not saying it is a good thing or that you should do it, only that
it is not a legal operation.

"If you're lucky, you'll get a segfault. ....."

Again I am not suggesting he assume a segfault will occur, only that he
needs to do the bounds checking himself.

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      01-03-2005
On 3 Jan 2005 10:19:39 -0800, in comp.lang.c , "gooch"
<> wrote:

(contextless stuff)

please read the VERY FIRST THING that richard said in his last post to you.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2005
"gooch" <> writes:
> You can index up to whatever value you want. p[1000] is fine. The
> result is going to be undefined. In most cases you would get a seg
> fault but that may not be the case. You have to do the bounds checking
> yourself.


(I presume p is an array with fewer than 1001 elements.)

p[1000] is not "fine" in any sense of the word. It is a serious error
that the implementation is not required to diagnose. This can
sometimes have the same visible results as something that's perfectly
legal, but the distinction is critical.

Incidentally, it's not true that "in most cases you would get a seg
fault". Attempting to access an array beyond its bounds is very
likely to refer to some other variable in your program, with
unpredictable results.

Your overall point, that you have to do your own bounds checking
because the implementation won't do it for you, is quite correct; I'm
just (over)reacting to your use of the word "fine".

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Stan Milam
Guest
Posts: n/a
 
      01-04-2005
Jack Klein wrote:

> On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <>
> wrote in comp.lang.c:
>
>
>>Hi Folks,
>>
>>
>>#include<stdio.h>
>>int main()
>>{
>> int (*p)[10];
>> int arr[10];
>> int i;
>>
>> p = arr; /* <-- compiler warning */

>
>
> The name of an array in an expression, other than when it is the
> operand of the 'sizeof' or '&' operators, is converted to a pointer to
> its first element. So in the statement above, you are assigning the
> address of an int (arr[0]) to a pointer to an array. But arr[0] is an
> int, not an array of ints.
>
> Replace with:
>
> p = &arr;
>
>
>> for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
>> *((*p) + i) = i;
>>
>> printf("%d\n", p[0][i]);
>> }
>>
>> return 0;
>>}
>>
>>In the above program compiler is giving following warning
>>
>>$gcc pointer2array.c
>>pointer2array.c: In function `main':
>>pointer2array.c:8: warning: assignment from incompatible pointer type
>>
>>Why the warning?
>>
>>O'kay and what is the use of int (*p)[10] type of declaration ?
>>Does the compiler perform any check on the array bounds?
>>lets say if i declare int arr[12]...


Okay, both of you are mistaken. int (*p)[10] is declaring an array of
10 pointers to function that return integers. Not quite the same thing
as either of you are talking about.

--
Regards,
Stan Milam.
-----------------------------------------------------------------------------
Vita Brevis. Carpe Guitarum! - Jamie Kinscherff
-----------------------------------------------------------------------------
 
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
what is it for an array ? int f(int VAR) { int tab[VAR]; .. } Alain Spineux C Programming 6 05-17-2011 01:03 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



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