473,748 Members | 2,602 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to an int array

Neo
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
Nov 14 '05 #1
22 2226
On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <ti************ ***@yahoo.com>
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Neo

"Jack Klein" <ja*******@spam cop.net> wrote in message
news:db******** *************** *********@4ax.c om...
On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <ti************ ***@yahoo.com>
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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Nov 14 '05 #3
On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
<ti************ ***@yahoo.com> 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 =----
Nov 14 '05 #4
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.

Nov 14 '05 #5
"gooch" <go******@comca st.net> 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" <ti************ ***@yahoo.com> 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
Nov 14 '05 #6
"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.

Nov 14 '05 #7
On 3 Jan 2005 10:19:39 -0800, in comp.lang.c , "gooch"
<go******@comca st.net> 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 =----
Nov 14 '05 #8
"gooch" <go******@comca st.net> 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_Keit h) ks***@mib.org <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.
Nov 14 '05 #9
Jack Klein wrote:
On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <ti************ ***@yahoo.com>
wrote in comp.lang.c:

Hi Folks,
#include<stdi o.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
-----------------------------------------------------------------------------
Nov 14 '05 #10

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

Similar topics

3
2360
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
204
13059
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
28
2404
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
1
3103
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
8
2237
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
617
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a platform which has the following properties: 1) char's are 8-Bit. ( "char" is synomonous with "byte" ). 2) int's are 32-Bit. ( sizeof(int) == 4 ). 3) Pointers are 64-Bit. ( sizeof(int*) == 8 ).
17
3259
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
12
3881
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
42
5337
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
26
4876
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
0
8984
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.