473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

question about pointer define

Hello,

I'm confused by the pointer definition such as int *(p[3]);

It seems if the parenthesis close p[3], it defines only 3 integers. The
star
is just useless. It can be showed by my program:

int main()
{
int *(p[3]);
// cout << *p[0] << "\t" << *p[1] << "\t" << *p[2] << endl;
cout << &p[0] << "\t" << &p[1] << "\t" << &p[2] << endl;
cout << p[0] << "\t" << p[1] << "\t" << p[2] << endl;
}

The program can be compiled on cygwin linux by g++ and run, the output
is:

0x22efb0 0x22efb4 0x22efb8
0x76c 0xffffffff 0x22efc8

If you delete the * in the definition, define int (p[3]); the second
line
will be:

1900 -1 2289608

Which is exactly the decimal numbers of the the previous program.

The second line is comment-out, coz it'll be core dumped if you want to
get
the content of the pointer.

Could someone explain what's going on here? Thanks.

Nov 15 '05
28 2411

Keith Thompson wrote:
Note that we considered this conversion rule in two different
contexts. For p, the prefix to the indexing operator, the rule
doesn't apply, because p is already a pointer (to an array of 3 ints).
For p[0], an array of 3 ints, the rule does apply, and it's converted
to a pointer-to-int.

In fact, after reading your program and explain, I doubt the compiler
just output the address of a pointer points to, no matter you are using
p or &p. The following program can prove it:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *p = NULL;
p = malloc(sizeof *p);
if (p == NULL)
{
exit(EXIT_FAILU RE);
}
*p = 10;
printf("p=%p\n" , (void*)p);
printf("&p=%p\n ", (void*)p);
printf("*p=%d\n ", *p);
free(p);
return 0;
}

It can be compiled by gcc on Linux, and the output is:

p=0x8049678
&p=0x8049678
*p=10

In the second printf, we apply the "&" (address-of) operator to p[0].
This is one of the contexts in which the conversion to pointer
*doesn't* take place. (Another is the operand of the sizeof
operator.) So &p[0] is the address of an array-of-3-ints. p[0] and
&p[0] have different types, but they yield the same raw pointer value
when converted to void* and printed with "%p".

Could you explain in which contexts such conversion doesn't take place?
And in your original program, p[1] actually refers to *another* array
of 3 ints, the second element of the array of arrays-of-3-ints to
which p points. In my version of your program, I only allocated
enough memory for a single array-of-3-ints; if I wanted to play with
p[1], I'd have to allocate more memory.

So, does that mean we have 3 pointers p[0], p[1], p[2], and each one
points
to an array of 3 integers? But we are not defining an array of
pointers, right?
This all seems confusing because, frankly, it is. I went down a
couple of blind alleys while writing this, and I'm not at all certain
that there are no errors in what I've written.

There really aren't many cases where it makes sense to have a pointer
to an array of 3 ints. Usually it makes much more sense to have a
pointer to int, which can be used to access an array of ints. If you
really need an array of items, where each item contains 3 ints, you
might be better off wrapping the array-of-3-ints in a struct and
declaring a pointer to the struct type. Even if you can keep the
complications of a pointer-to-fixed-size-array-of-whatever straight in
your head, the next person to read your code may not be able to.

For a better general understanding of the relationship between arrays
and pointers, read section 6 of the C FAQ,
<http://www.eskimo.com/~scs/C-faq/faq.html>. (In fact, read the whole
thing if you haven't already.)

And before assuming that everything I've written here is correct, wait
a while until the other regulars have had a chance to pick it apart.


You are absolutely right. I never use pointers to an array. Here I just
want to make the concept clear.
Thanks a lot. You are so nice!

Nov 15 '05 #21

Jack Klein wrote:
Please provide context when you follow-up or reply to a post. There
have been any number of posts around here explaining how to do this
properly using the broken Google groups, or get yourself a real
newsreader.

Sorry about that. Actually, I'm using Outlook Express 6 to read, but
the free NNTP server news://freenews.netfront.net doesn't allow me to
post. That's why I have to use Google groups to post. Do you know any
free news server allows post? Thanks.
The issue about C++ code is this: your sample program is written in
C++, not in C. Your original question was about the meaning of the
declaration 'int *(p[3]);'. We cannot answer that question here in
the context of C++, only in the context of C, which does not apply to
your sample program.


I understand what you said. I'll stick to pure C in this group in the
future. Thanks.

Nov 15 '05 #22
"Wonder" <sa******@gmail .com> wrote:
Keith Thompson wrote:
Note that we considered this conversion rule in two different
contexts. For p, the prefix to the indexing operator, the rule
doesn't apply, because p is already a pointer (to an array of 3 ints).
For p[0], an array of 3 ints, the rule does apply, and it's converted
to a pointer-to-int.
In fact, after reading your program and explain, I doubt the compiler
just output the address of a pointer points to, no matter you are using
p or &p. The following program can prove it:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *p = NULL;
p = malloc(sizeof *p);
if (p == NULL)
{
exit(EXIT_FAILU RE);
}
*p = 10;
printf("p=%p\n" , (void*)p);
printf("&p=%p\n ", (void*)p);


ITYM:
printf("&p=%p\n ", (void*)&p);
printf("*p=%d\n ", *p);
free(p);
return 0;
}

It can be compiled by gcc on Linux, and the output is:

p=0x8049678
&p=0x8049678
Which will be different with the code correction above applied.
*p=10

In the second printf, we apply the "&" (address-of) operator to p[0].
This is one of the contexts in which the conversion to pointer
*doesn't* take place. (Another is the operand of the sizeof
operator.) So &p[0] is the address of an array-of-3-ints. p[0] and
&p[0] have different types, but they yield the same raw pointer value
when converted to void* and printed with "%p".
Could you explain in which contexts such conversion doesn't take place?


An array "decays" into a pointer, except when it is the operand of the
address operator & or the sizeof operator. (Another exception are
string literals used to initialize arrays.) Consider:

int arr[42];

arr /* arr is converted to a pointer to its first element, the
type of the expression is pointer-to-int. */

arr[1] /* arr is again converted, the value of the expression is
*(arr+1), its type is int. */

&arr /* No conversion of arr, value of the expression is the same
as above (the address of the array equals the address of
its first element). The type however is pointer-to-array.
*/

sizeof arr /* No conversion, value is the size of the array. */
Now back to our original example:

int (*p)[3] /* p is a pointer-to-array-of-three-ints */

p[0] /* Per definition, this is equivalent to: *(p + 0),
which yields an array, which in turn decays into a
pointer to it's first element, a pointer-to-int. */

&p[0] /* Per definition, this is equivalent to (p + 0), a
pointer-to-array-of-three-ints. */

Note that the two latter expressions yield the same value (memory
address), but are of different type!
And in your original program, p[1] actually refers to *another* array
of 3 ints, the second element of the array of arrays-of-3-ints to
which p points. In my version of your program, I only allocated
enough memory for a single array-of-3-ints; if I wanted to play with
p[1], I'd have to allocate more memory.


So, does that mean we have 3 pointers p[0], p[1], p[2], and each one
points
to an array of 3 integers? But we are not defining an array of
pointers, right?


Right, but remember that the array subscription operator [] is nothing
but syntactic sugar for *(p + index). Thus we can use array
subscription on any old object pointer if we like. However, in the
mentioned case, we indeed refer to memory we don't own.
[...] I never use pointers to an array. [...]


A wise decision, since they are hardly useful anyway. ;-)

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #23
"Wonder" <sa******@gmail .com> writes:
Keith Thompson wrote:
Note that we considered this conversion rule in two different
contexts. For p, the prefix to the indexing operator, the rule
doesn't apply, because p is already a pointer (to an array of 3 ints).
For p[0], an array of 3 ints, the rule does apply, and it's converted
to a pointer-to-int.


In fact, after reading your program and explain, I doubt the compiler
just output the address of a pointer points to, no matter you are using
p or &p. The following program can prove it:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int *p = NULL;
p = malloc(sizeof *p);
if (p == NULL)
{
exit(EXIT_FAILU RE);
}
*p = 10;
printf("p=%p\n" , (void*)p);
printf("&p=%p\n ", (void*)p);
printf("*p=%d\n ", *p);
free(p);
return 0;
}

It can be compiled by gcc on Linux, and the output is:

p=0x8049678
&p=0x8049678
*p=10


You have a typo; your first and second printf() calls both print the
same value. After changing the second printf() to
printf("&p=%p\n ", (void*)p);
I get:

p=0x460210
&p=0x22ef14
*p=10
In the second printf, we apply the "&" (address-of) operator to p[0].
This is one of the contexts in which the conversion to pointer
*doesn't* take place. (Another is the operand of the sizeof
operator.) So &p[0] is the address of an array-of-3-ints. p[0] and
&p[0] have different types, but they yield the same raw pointer value
when converted to void* and printed with "%p".


Could you explain in which contexts such conversion doesn't take place?


Quoting the standard (C99 6.3.2.1p3):

Except when it is the operand of the sizeof operator or the unary
& operator, or is a string literal used to initialize an array, an
expression that has type "array of type" is converted to an
expression with type "pointer to type" that points to the initial
element of the array object and is not an lvalue. If the array
object has register storage class, the behavior is undefined.
And in your original program, p[1] actually refers to *another* array
of 3 ints, the second element of the array of arrays-of-3-ints to
which p points. In my version of your program, I only allocated
enough memory for a single array-of-3-ints; if I wanted to play with
p[1], I'd have to allocate more memory.


So, does that mean we have 3 pointers p[0], p[1], p[2], and each one
points to an array of 3 integers? But we are not defining an array
of pointers, right?


Yes and no. It depends on what you mean by "pointer". The term
usually refers to a pointer *object*, but it can also refer to a
pointer *value* (also known as an address) -- yet another source of
confusion.

Recall that p is a pointer to array-of-3-ints. So let's say we
allocate enough memory for 3 such arrays:

p = malloc(9 * sizeof int);

Then each of p[0], p[1], and p[2] is an array of 3 ints. p itself is
the only pointer object in sight, but you can construct a pointer to
(the address of) any object. It happens that the expression p[1]
evaluates to a pointer value (because of the implicit array-to-pointer
conversion), but p[1] itself is an array object.

--
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 15 '05 #24
Keith Thompson <ks***@mib.or g> wrote:
"Wonder" <sa******@gmail .com> writes: <snip>
printf("p=%p\n" , (void*)p);
printf("&p=%p\n ", (void*)p);

<snip>You have a typo; your first and second printf() calls both print the
same value. After changing the second printf() to
printf("&p=%p\n ", (void*)p);

<snip>

Cough. Change? Which change? ;^)

Best regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc frequent answers: http://benpfaff.org/writings/clc.
Nov 15 '05 #25
Irrwahn Grausewitz <ir*******@free net.de> writes:
Keith Thompson <ks***@mib.or g> wrote:
"Wonder" <sa******@gmail .com> writes:

<snip>
printf("p=%p\n" , (void*)p);
printf("&p=%p\n ", (void*)p);

<snip>
You have a typo; your first and second printf() calls both print the
same value. After changing the second printf() to
printf("&p=%p\n ", (void*)p);

<snip>

Cough. Change? Which change? ;^)


D'oh!

--
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 15 '05 #26
On Thu, 08 Sep 2005 22:38:43 +0000, Malcolm wrote:

"Wonder" <sa******@gmail .com> wrote

I'm confused by the pointer definition such as int *(p[3]);

It seems if the parenthesis close p[3], it defines only 3 integers. The
star
is just useless. It can be showed by my program:
Everyone else is also confused, except maybe a few C rules lawyers.

The problem is that C allows you to define pointers to complex compound
types, such as arrays of arbitrary dimensions. The syntax very rapidly
becomes non-human understandable.

Here we have a pointer to an array of three integers.


int *(p[3]) declares p as an array of 3 pointers to int, it is not a
pointer to an array. Since [] naturally binds more tightly than * the
parentheses here are redundant, and it is equivalent to int *p[3]. A
pointer to an array of 3 ints would be written as int (*p)[3].
The declaration int
*p[3] would be an array of three pointers to an integer.
However under the bonnet a pointer to an array of three integers and a
pointer to an integer (which may have other integers follwing it
contiguously in memory) is the same. To avoid the confusion that results, a
wise C programmer will always use a plain int *, and specify in the comment
above the function if necessary that the pointer must point to exactly three
integers, or to a list of triplets if that is the intention.


The 3 types int *, int *[3] and int (*)[3] have different properties, you
cannot simply substitute one for another. int * will NOT do when you want
int *[3]. As for int (*)[3] it is used for arrays of arrays e.g.

int a[4][3];
int (*p)[3] = a;
int (*q)[3] = malloc(4 * sizeof *q);

if (q == NULL) { /* Deal with error */ };

Now p[2][1] will access element a[2][1], q[2][1] will do the same thing
for a dynamically allocated array of arrays. Making p or q int * will not
work here.

Lawrence



Nov 15 '05 #27
Wonder wrote:
Jack Klein wrote:
Please provide context when you follow-up or reply to a post. There
have been any number of posts around here explaining how to do this
properly using the broken Google groups, or get yourself a real
newsreader.

Sorry about that. Actually, I'm using Outlook Express 6 to read, but
the free NNTP server news://freenews.netfront.net doesn't allow me to
post. That's why I have to use Google groups to post. Do you know any
free news server allows post? Thanks.


I would use Google and search 'free news server' or something like that.
Do you use an ISP that doesn't supply news? Do you get mail?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #28
"Wonder" <sa******@gmail .com> writes:
Jack Klein wrote:
Please provide context when you follow-up or reply to a post. There
have been any number of posts around here explaining how to do this
properly using the broken Google groups, or get yourself a real
newsreader.


Sorry about that. Actually, I'm using Outlook Express 6 to read, but
the free NNTP server news://freenews.netfront.net doesn't allow me to
post. That's why I have to use Google groups to post. Do you know any
free news server allows post? Thanks.


No, I don't. (I think news.individual .net recently stopped offering
free accounts.)

Most ISPs offer Usenet access; does yours?

If not, you can still use groups.google.c om. It makes it
gratuitiously difficult, but not impossible, to post properly.

If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
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 15 '05 #29

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

Similar topics

11
25770
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression being used (for example, int* someInt=NULL; ). I used similar initialization myself, and it works fine even if I don't define NULL. But what is "NULL" exactly? Is it a constant defined by compiler? Is there any difference between the following two...
33
619
by: Mohanasundaram | last post by:
Hi All, As per the standard what is the result of passing NULL to both malloc and free? Regards, Mohan. http://www.gotw.ca/resources/clcm.htm for info about ]
3
1673
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can you please verify for me? Usually, I write function that is about 100 lines. I would need 20 parameters which they are all reference to global variable inside struct. For best optimization, it does not require to create stack frame because...
36
7791
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption about the most restrictive type on my machine? Thanks.
9
3529
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin code*/
2
4180
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin code*/
24
2106
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the first line, "warning C4132: 'x' : const object should be initialized" yet generates correct code.
10
4126
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
10
1572
by: masood.iqbal | last post by:
The code example below shows the dynamic allocation of a 2D array. I must admit that it took quite a while for me to get there (I already have another posting to that effect), but I am glad that I finally got it working. Now here's the problem: I am able to get the 2D array dynamically allocated correctly as long as I am doing it "in-line" (i.e. without invoking any function). The moment I try to do it in another function, I get a...
18
2129
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for last_values as well as assign the value of a pointer to the assigned space. Which I think I'm doing by using:
0
9589
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
9423
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
10211
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
10045
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
9994
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
8872
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
6673
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
5299
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...
3
2815
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.