473,769 Members | 2,100 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 2212

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
2361
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
13092
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
2411
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
3105
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
3262
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
3885
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
5346
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
4881
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
9422
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
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10038
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...
1
9987
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
9857
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...
1
7404
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
6662
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
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2812
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.