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

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 2175

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 misunderstanding 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 07:22:28 +0000, Martin Ambuhl wrote:
[...]
int (*arrptr)[5];
int arr[5] = { 1, 2, 3, 4, 5 }; [...] arrptr = (int (*)[5]) arr;


Or just:
arrptr = &arr;

--
http://members.dodo.com.au/~netocrat

Nov 15 '05 #9
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 #10
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 #11
kl*****@xs4all.nl writes:
blue schreef:
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.


I don't believe this is one of the areas in which C and C++ differ.

[...]
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.


No, the first argument to printf() is a pointer-to-char, *not* a
pointer-to-array. Specifically, it's a pointer to the first element
of an array of char (a string); the entire array can be accessed via
that pointer.

--
Keith Thompson (The_Other_Keith) 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 15 '05 #12
Flash Gordon wrote:
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;
I was still asleep or I would have corrected this to
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 #13
Michael Mair <Mi**********@invalid.invalid> writes:
blue wrote:
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.


Yes, that's correct, but it doesn't obviously follow from the example
with a pointer-to-int until you take into account the special rules
for arrays and pointers.

Given the above declarations, the assignment
p = i;
is incorrect because p is a pointer and i is an int.

The assignment
arrptr = arr;
is also incorrect, but both sides of the assignment are pointer types,
just not compatible pointer types. The expression "arr" is an array
name; it's implicitly converted to a pointer to the first element.

But the correct statement is
arrptr = &arr;
because the operand of a unary "&" operator is one of the few contexts
in which an array name is *not* implicitly converted to a pointer.
"&arr" gives you the address of the whole array.

--
Keith Thompson (The_Other_Keith) 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 15 '05 #14
[snips]

On 12 Oct 2005 00:31:17 -0700, ma*******@gmail.com wrote:
C will not detect array index out-of-bound.


There's no guarantee that it won't, AFAIK. It's not _required_ to, but
that's not the same as saying it won't.
Nov 15 '05 #15

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

Similar topics

3
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. ...
204
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 =...
28
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: ...
1
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
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
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...
17
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: ...
12
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...
42
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.