473,769 Members | 6,187 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

Wonder wrote:
i don't think the elements in p are pointers, coz you even can't access
*p[i], i=0,1,2

Because you haven't initialized them to point anywhere meaningful yet.
If you had done something like:

int x, y, z;
int *(p[3]) = {&x, &y, &z};

or
int *(p[3]);
p[0] = &x;
p[1] = &y;
p[2] = &z;

or even

p[0] = malloc(sizeof *p[0]);
p[1] = malloc(sizeof *p[1]);
p[2] = malloc(sizeof *p[2]);

*then* you could dereference each element.
if you make the parentheses close *p, or just erase parentheses, such
as
int (*p)[3] or int *p[3]

no problem, it's the definition of an array of three pointers to int.

No. Those two declarations do entirely different things:

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

In the case of

int *(p[3]);

or

int (*p[3]);

the parentheses are redundant and unnecessary; they do not add anything
to the declaration, and are the same as int *p[3]. So remember:

int p[3]; // p is a 3-element array of int;
int *p[3]; // p is a 3-element array of pointer to int,
int *(p)[3]; // same as above
int *(p[3]); // same as above
int (*p[3]); // same as above
int (*p)[3]; // p is a pointer to a 3-element array of int
BTW: is there a rule say that only C program can be written here, even
though the question is about C in nature?


Don't assume that everyone who knows C also knows C++ as well; it's
kind of like asking a question about Latin but using French to
illustrate the problem. C and C++ are just different enough that using
one to ask questions about the other could lead to misunderstandin gs
and mistakes.

Nov 15 '05 #11
"Wonder" <sa******@gmail .com> wrote:

[ about int *(p[3]); ]

Please preserve some context when you reply.
i don't think the elements in p are pointers,
They are. Just read the definition inside out, following the
precedence rules:

p p is ...
p[3] ... an array of three elements ...
(p[3]) ... (the ()s (are (just (noise)))) ...
*(p[3]) ... of pointers ...
int *(p[3]) ... to int. QED.
coz you even can't access
*p[i], i=0,1,2
But only because you didn't fill the array with sensible values.
if you make the parentheses close *p, or just erase parentheses, such
as
int (*p)[3] or int *p[3]

no problem, it's the definition of an array of three pointers to int.
Err, yes for the latter, no for the former:

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

It seems you're in dire need of a good C textbook.
BTW: is there a rule say that only C program can be written here, even
though the question is about C in nature?


Only C programs are of C in nature. ;-)

In other words: if you post code here, make sure it's C.
Additionally, you might want to follow the links in my signature
for an intersting reading.

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 #12
> p p is ...
(*p) ... a pointer ...
(*p)[3] ... to an array of three ...
int (*p)[3] ... ints.
Thanks a lot! Your method is very useful.
In other words: if you post code here, make sure it's C.

I rewrite the program in pure C, here it is:

#include <stdio.h>
int main()
{
int (*p)[3];
/* printf("%x\t%x\ t%x\n",*p[0],*p[1],*p[2]); */
printf("%x\t%x\ t%x\n",p[0],p[1],p[2]);
printf("%x\t%x\ t%x\n",&p[0],&p[1],&p[2]);
}

And I recompiled it by gcc under cygwin. The output is:
4 10 1c
4 10 1c

The first printf would cause core dump, but now I can understand it if
here p is a pointer to an int array which has 3 elements.
My question is why the second and third printf give the same result,
seems p[i] is same as &p[i] here.
Thanks.

Nov 15 '05 #13

Wonder wrote:
Thanks a lot for your kind reply.

Now the problem goes to the difference between
int (*p)[3];
int *p[3];
int *(p[3]);

As you said, the second and the third are same. You are right, for
both, the *p[i] will give a core dump. However, the first one just give
the values of the elements, even though I didn't initialize anything.

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 output is:
440 -1869574000 -1869574000
0x6101cf3e 0x6101cf4a 0x6101cf56
0x6101cf3e 0x6101cf4a 0x6101cf56

Another strange thing is the second line and third line are same in
this case.


IIRC, dereferencing an invalid or NULL pointer invokes undefined
behavior, meaning any output you get will be suspect.

As a matter of habit, I explicitly initialize pointers to NULL or a
valid memory location when I declare them, just to avoid these kinds of
problems.

Nov 15 '05 #14
"Wonder" <sa******@gmail .com> writes:
I didn't copy & paste the header coz I thought it had nothing to do
with the question itself, and I don't want to waste too many lines.


It's (almost) always best to post a small, complete, working,
self-contained program, something that the rest of us can
cut-and-paste and try for ourselves. If your program uses printf(),
add the required "#include <stdio.h>".

A missing include directive can cause unpredictable problems, problems
that your compiler won't necessarily diagnose. If you post what
appears to be an incomplete program, it's always possible that the
problem is in the part you didn't post. (If you knew enough to be
absolutely sure that that's not the case, you wouldn't be asking us
about it.)

Be sure to cut-and-paste the exact code that you compiled. If you
manually transcribe it, you're likely to introduce errors, and we'll
have no way of guessing where the real problem is if we can't see your
actual code.

And yes, posting C++ to comp.lang.c is in appropriate. Some of us
might happen to know what "cout << endl" means, but some of us may
not.

You're using groups.google.c om, which unfortunately makes it far too
easy to post improperly. Don't assume that your readers can easily
see the article to which you're replying. Include enough quoted text
so someone can read each followup on its own, post your new text
*after* any quoted text, and provide proper attributions so we can
tell who said what. There is a way to do this (and this has been
posted here hundreds of times):

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.

Finally, we get a lot of people coming here and making these same
mistakes. For each new poster, it's the first time; for us regulars,
it's the same thing over and over and over again. Sometimes the
frustration boils over a bit. The best solution is for new posters to
follow the newsgroup for a while, or browse the archives, to get an
idea of how things are done. Unfortunately, there doesn't seem to be
a good way to get the message out.

--
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 #15
"Wonder" <sa******@gmail .com> writes:
Irrwahn Grausewitz <ir*******@free net.de> writes:

[attribution added]
p p is ...
(*p) ... a pointer ...
(*p)[3] ... to an array of three ...
int (*p)[3] ... ints.


Thanks a lot! Your method is very useful.
In other words: if you post code here, make sure it's C.

I rewrite the program in pure C, here it is:

#include <stdio.h>
int main()
{
int (*p)[3];
/* printf("%x\t%x\ t%x\n",*p[0],*p[1],*p[2]); */
printf("%x\t%x\ t%x\n",p[0],p[1],p[2]);
printf("%x\t%x\ t%x\n",&p[0],&p[1],&p[2]);
}

And I recompiled it by gcc under cygwin. The output is:
4 10 1c
4 10 1c

The first printf would cause core dump, but now I can understand it if
here p is a pointer to an int array which has 3 elements.
My question is why the second and third printf give the same result,
seems p[i] is same as &p[i] here.


Let's try a modified version of your program.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int (*p)[3];
p = malloc(sizeof *p);
if (p == NULL) {
exit(EXIT_FAILU RE);
}
printf("p[0] = %p\n", (void*)p[0]);
printf("&p[0] = %p\n", (void*)&p[0]);
free(p);
return 0;
}

On one implementation, I get:

p[0] = 0x460210
&p[0] = 0x460210

As we've established, p is a pointer to an array of 3 ints.

You didn't initialize p, so its value is garbage. Any attempt to look
at whatever it points to invokes undefined behavior (I got a segmentation
fault). I've corrected this with the malloc() call.

The correct format for printing a pointer value is "%p", not "%x".
The "%p" format expects an argument of type void*; the casts are
necessary (even though you may be able to get away without them).

The prefix to an indexing operator [] is actually a pointer. If it's
the name of an array, as in
int arr[10];
arr[5];
the array name is implicitly converted to a pointer to its first
element. Since p is already a pointer, no such conversion is
necessary.

Remember that, by definition, x[y] is equivalent to *(x+y). In this
case, p[0] is equivalent to *(p+0), which is equivalent to *p. So the
expression p[0] gives you the thing that p points to, which happens to
be an array of 3 ints.

So p[0] is an array of 3 ints. In most contexts, an expression of
array type is implicitly converted to a pointer to its first element.
So in the printf() statement, p[0] is converted to a pointer-to-int,
and its value happens to be 0x460210. (Actually, it's pseudo-random
garbage, but we'll leave that aside for now.)

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 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".

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.

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.

--
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 #16
Wonder wrote:
You just show the rudeness
which should not represent the spirit of comp.lang.c.


It doesn't.
You have just met some of our indigenous trolls.
They pounce on every new subscriber.
You must learn to recognize them and ignore them.
Nov 15 '05 #17
On 8 Sep 2005 11:26:40 -0700, "Wonder" <sa******@gmail .com> wrote in
comp.lang.c:
i'm just using cout to output. It has nothing to do with the nature of
the question here.
I'm new here, don't know we have to write strict C code.


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.

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.

Whether the meaning of the declaration in C++ is the same as it is in
C is a matter for comp.lang.c++, since C does not define any
compatibility with C++. It is C++, rather, that defines backwards
compatibility with C. The C++ language standard incorporates parts of
an older, out-of-date version of the C standard, not vice-versa.

So while posters in this group could explain what the declaration
means in C, that is not necessarily what it means in your C++ (or Java
or C# of FORTRAN or ADA) program.

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

"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. 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.

Nov 15 '05 #19
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Wonder wrote:
You just show the rudeness
which should not represent the spirit of comp.lang.c.


It doesn't.
You have just met some of our indigenous trolls.
They pounce on every new subscriber.
You must learn to recognize them and ignore them.


ERT himself is our most persistent indigenous troll. He has a long
history of posting misinformation.

--
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 #20

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...
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...
1
7409
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
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.