LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-02-2004, 10:47 AM   #1
xailer
Member
 
Registered: Nov 2003
Posts: 77

Rep: Reputation: 15
Initialization of a pointer in C-a question


hi

I came accross an example where pointer is initialised directly from integer:

int x=1;
int *px=x; //notice it doesn't use &x
printf("%d",(int)px);

What is happening here ? How can pointer get address from x ?
And if you can get value of x by just casting poinetr to int ,that would mean that px saved a value of an integer and not its address .

And what is the benefit of doing it like that ? Is assigment any faster ?

thank you

bye
 
Old 01-02-2004, 11:13 AM   #2
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
the pointer aint getting the address of x, it's getting it's value

its excly the same of

char px=x;
printf("%d",(int)px);

doensn't matter the type of px you're just casting


>> And what is the benefit of doing it like that ?

none, just more confusing to who reads the code
 
Old 01-02-2004, 11:42 AM   #3
nibbler
Member
 
Registered: Oct 2003
Location: Croatia
Distribution: Fedora 4
Posts: 46

Rep: Reputation: 15
i think gcc wouldn't allow that
 
Old 01-02-2004, 11:46 AM   #4
xailer
Member
 
Registered: Nov 2003
Posts: 77

Original Poster
Rep: Reputation: 15
thank you for your help

bye
 
Old 01-02-2004, 12:57 PM   #5
luxitan
Member
 
Registered: Aug 2003
Location: Portugal
Distribution: Gentoo
Posts: 78

Rep: Reputation: 15
Quote:
Originally posted by nibbler
i think gcc wouldn't allow that
it will give a warning but it's valid
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer question, djgerbavore Programming 6 08-13-2004 02:21 AM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM
Pointer Question AMMullan Programming 15 01-10-2004 05:40 PM
yet another C pointer question fatman Programming 3 04-18-2003 04:59 PM
Initialization Question. Crossfire Linux - Newbie 3 07-27-2002 01:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration