473,698 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a pointer to an array?

Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:

int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work? Besides, does anyone know the real usage of
array pointers?

Best regards,

blue

Nov 15 '05 #1
14 2201

blue schreef:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array.
In C++ i would recommend it. Unfortunately, it's not valid C.

(See also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:

int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work?
Coz' it's not C.
int main()
{
int *arrptr;
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}
Besides, does anyone know the real usage of array pointers?


There are many uses for them. printf, for instance, usually takes an
array pointer as it's first parameter, "%d" in your case. You can
calculate a pointer to any element in the array by arrptr=arr + i.
Except for the fact that they always point to some valid block of
memory (as long as they're in scope), array-pointers do not differ from
any other pointer.

regards,

Kleuske

Nov 15 '05 #2
blue wrote:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:
#include <stdio.h>
This is _not_ gratuitous.
int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
What is that? If you have
int *p, i;
then you use
p = &i;
So, your compiler should tell you that
arrptr = arr;
is wrong. Using the above,
arrptr = &arr;
is right.
printf( "%d", arrptr[0] );
Instead of p[0], you can also write *p. So, essentially, you
are trying to print out an array using %d. I hope you are
aware that this is a stupid idea.
What you probably want is
arrptr[0][0]
or, in this case,
(*arrptr)[0]

Notes: You could also use arrptr to access an array N of an
array 5 of int -- maybe this helps you better to understand
the "[0][0]"...
Another thing: To make this program portable, append '\n'
to your last output, i.e. "%d\n" or a separate putchar('\n');
You forgot to
return 0; }

Why could it not work? Besides, does anyone know the real usage of
array pointers?


See above. Yes.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
kl*****@xs4all. nl wrote:
blue schreef:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array.


In C++ i would recommend it. Unfortunately, it's not valid C.


This is wrong.

In comp.lang.c, I do not officially know about C++ but, used
correctly, "this" is definitely valid C -- the reason why
the whole thing went into the _comp.lang.c_ FAQ.

See also my other reply to this thread.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #4
Thanks for your reply, but I have some questions. Do you mean that if I
declared an array pointer, I will never have "array index out-of-bound"
problem? Can C (C++ you meantioned) do such examinations? I think only
object-oriented language, such as JAVA, will do.

Best regards,

blue

Nov 15 '05 #5
blue wrote:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
If you really need to declare a pointer to an entire array, use
something like "int (*ap)[N];" where N is the size of the array. (See
also question 1.21.) If the size of the array is unknown, N can in
principle be omitted, but the resulting type, "pointer to array of
unknown size," is useless.
However, when I try to write a sample program like:

int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work? Besides, does anyone know the real usage of
array pointers?


#include <stdio.h>

int main(void)
{
int (*arrptr)[5];
int arr[5] = { 1, 2, 3, 4, 5 };
unsigned i;
arrptr = (int (*)[5]) arr;
for (i = 0; i < sizeof arr / sizeof *arr; i++)
printf("%d\n", (*arrptr)[i]);
return 0;
}

1
2
3
4
5
Nov 15 '05 #6
Hi, Michael:

I have tried what you suggested, it did work! However, It seems that
I have some misunderstandin g of array and pointers:

int arr[5] = {1,2,3,4,5};
int (*arrptr)[5];

Does it not mean that the variable "arr" itself is an "implicit"
pointer? So we could use it like *arr, *(arr+1), ... Besides, for an
array, does "&arr" really exist?

Then why I assign arr to arrptr is an illegal operation? Only "arrptr =
&arr" is legal.

Best regards,

blue

Nov 15 '05 #7

blue wrote:
Thanks for your reply, but I have some questions. Do you mean that if I
declared an array pointer, I will never have "array index out-of-bound"
problem? Can C (C++ you meantioned) do such examinations? I think only
object-oriented language, such as JAVA, will do.

Best regards,

blue


C will not detect array index out-of-bound.
But compiler will throw a warning if you try to assign address of a
different sized array to the pointer.

Nov 15 '05 #8
On Wed, 12 Oct 2005 00:27:57 -0700, blue wrote:

[...]
int arr[5] = {1,2,3,4,5};
int (*arrptr)[5];

Does it not mean that the variable "arr" itself is an "implicit"
pointer? So we could use it like *arr, *(arr+1),
In most contexts, the array "arr" decays into a pointer, so generally yes
it can be used as you say. Read the FAQ for more details. This is often
referred to as "the rule", at least in this group.
... Besides, for an
array, does "&arr" really exist?
It does, yes. This has been discussed on the list in the past, and
although they are different types, for meaningful conversions[*] we can
say that the value of arr and &arr is always the same.
Then why I assign arr to arrptr is an illegal operation? Only "arrptr =
&arr" is legal.


Because they are different and incompatible types. arr is "array 5 of
int" and &arr is "pointer to array 5 of int".
[*] those conversions being at least to void * and probably char *.
i.e. (void *)arr == (void *)&arr
(char *)arr == (char *)&arr

--
http://members.dodo.com.au/~netocrat
Nov 15 '05 #9
blue wrote:
Hi, all:
Recently I found this comment on comp.lang.c "Answers to FAQ" (6.13):
<snip>
However, when I try to write a sample program like:
printf requires a prototype otherwise your program invokes undefined
behaviour and anything can happen.

#include <stdio.h>
int main()
{
int (*arrptr)[5];
int arr[5] = {1,2,3,4,5};
arrptr = arr;
printf( "%d", arrptr[0] );
}

Why could it not work? Besides, does anyone know the real usage of
array pointers?


You should always say what you mean by "does not work". In this case I
assume you mean that the compiler complains at you.

Think about what you would do with a pointer to an int. You would
dereference the pointer, would you not? The same applies to pointers to
arrays, if you want to read what they point to you need to dereference them.
printf( "%d", (*arrptr)[0] );
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #10

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

Similar topics

3
2356
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
13011
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
2390
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
3096
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
2233
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
3248
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
3878
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
5322
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
4861
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
8600
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,...
0
9018
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8858
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
7711
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...
1
6517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4360
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
4614
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3038
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2322
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.