473,756 Members | 2,977 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 #1
28 2407
Wonder wrote on 08/09/05 :
// 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;


Not a C-code. C++ is next door.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
Nov 15 '05 #2
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.

Nov 15 '05 #3
"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.
No. p is an array of three pointers to int.
The star is just useless.
Definitely not. The parentheses are.
It can be showed by my program:

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

<snip>

This is by no means a C program. Either rewrite in C, or take this to
comp.lang.c++, please.

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 #4
i don't think the elements in p are pointers, coz you even can't access
*p[i], i=0,1,2

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.

BTW: is there a rule say that only C program can be written here, even
though the question is about C in nature?

Thanks.

Nov 15 '05 #5

Wonder wrote:
Hello,

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

It's the same as

int *p[3];

i.e., a 3-element array of pointers to int.
It seems if the parenthesis close p[3], it defines only 3 integers. The
star
is just useless.
The '*' indicates that p is an array of pointers to int, instead of
just an array of int.
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.

Right. What's happening is that the contents of p are uninitialized,
so p[0] through p[2] contain random bit strings, and these random bit
strings happened to be the same between the two runs. In the first
case, where p is declared as

int *(p[3]);

each element of p is of a *pointer* type, so the output is formatted to
reflect that (i.e., as hex values corresponding to a virtual memory
address). In the second case, where p is declared as

int (p[3]);

each element is of an *int* type, and the output is formatted to
reflect that.
The second line is comment-out, coz it'll be core dumped if you want to
get
the content of the pointer.

Right, because without proper initialization, p[0] through p[2] don't
point anywhere meaningful.
Could someone explain what's going on here? Thanks.


Basically, for any type T and declarator foo,

T *(foo);

is the same as

T *foo;

Nov 15 '05 #6
Wonder wrote:
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.
Wrong.
The star is just useless.
Wrong.
It can be showed by my program:
You program shows nothing. It is nothing but a bunch of syntax errors.
If you had posted it to <news:comp.lang .c++> instead, it would still be
garbage: no inclusion of needed the header file, no specification of the
needed namespace qualification.

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++
Which is not a C compiler, therefore not topical in <news:comp.lang .c>.
And it won't compile under g++ if you invoke g++ with appropriate
specification of diagnostics and of the standard used.

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


You're an idiot.
Nov 15 '05 #7
Wonder wrote:
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.


Your code is not C. It is not loose C; it is not strict C; it is not
GNU C. It is in a different language.
Nov 15 '05 #8
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.

Nov 15 '05 #9
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.

You just show the rudeness which should not represent the spirit of
comp.lang.c.

Nov 15 '05 #10

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

Similar topics

11
25767
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
1672
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
7785
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
3524
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
4179
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
2105
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
4123
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
1569
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
9456
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
10040
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
9873
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
6534
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
5142
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3806
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
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.