LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 02-02-2012, 09:11 PM   #1
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 92

Rep: Reputation: Disabled
Beginning C programming- how to print memory locations? printf conversion number?


I am having a difficult time understanding pointers and how to use them. As part of my learning process, I like to see what happens when I change things- but I then need to print them out to see what (if any) change has occurred.

Here's a very basic little program as an example:

Code:
#include <stdio.h>

int main(void)
{
    float x;				/* x is of float type		*/
    float *fp;				/* fp is a pointer to a float	*/

    x = 6.5;				/* x now contains the value 6.5	*/

					/* print contents and address of x	*/
    printf("Value of x is %f, address of x %ld\n", x, &x);

    fp = &x;				/* fp now points to location of x	*/

					/* print value and contents of fp	*/
    printf("Value in memory location fp is %f\n", *fp);
    printf("address of fp (should be same as address of x) is %ld\n", fp);

	return 0;
}
Here's the compiler output:
Code:
tst.c: In function ‘main’:
tst.c:11:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 3 has type ‘float *’ [-Wformat]
tst.c:17:5: warning: format ‘%ld’ expects argument of type ‘long int’, but argument 2 has type ‘float *’ [-Wformat]
And here's the output from running the program:

Code:
Value of x is 6.500000, address of x 140737165116276
Value in memory location fp is 6.500000
address of fp (should be same as address of x) is 140737165116276
I've got a lot of warning flags for the compiler turned on, and I realize that programs can _work_ while not completely conforming to all the rules, but I'd like to learn how to do things correctly, and it will help me understand how things work.

Can you tell me what conversion specifier to use when printing out memory addresses (the value inside the pointer)? I haven't found a conversion specifier for doubles, and %f doesn't work (without errors) either.

Thanks,
Keith Ostertag
 
Old 02-02-2012, 09:56 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Use %p to print pointers; just cast the pointers to a void pointer first, (void *)pointer . For doubles, use %f -- printf does not support floats. They are both explained in the man page; see man 3 printf.

Note that for scanf, the type for floats is %f but %lf for doubles.

In your code above, change the two lines to
Code:
    printf("Value of x is %f, address of x %p\n", (double)x, (void *)&x);
and
Code:
    printf("address of fp (should be same as address of x) is %p\n", (void *)fp);
Note that to print a float pointed to by fp, you should use (double)(*fp) or (double)*fp . To treat fp as a pointer to a double, you'd use (double *)(fp) or (double *)fp -- but since double is larger than a float, you'd be referencing undefined memory, so don't.

I'd also like to say that it warms my heart to see someone actually care about compiler warnings!

Last edited by Nominal Animal; 02-02-2012 at 10:05 PM.
 
1 members found this post helpful.
Old 02-02-2012, 10:47 PM   #3
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 92

Original Poster
Rep: Reputation: Disabled
Thank you. I have been looking at the man pages for printf, scanf, etc, but at this stage they are still over my head- can't understand the terminology or syntax yet.

I had tried %p as a specifier but didn't know to "cast the pointers to a void pointer first". Don't yet know what "cast" means, I'm teaching myself via books and haven't come across that yet. Could you explain about casting or point to a web page describing its proper use?

I don't understand (void *)&x and (void *)fp - but its late and tomorrow I will play with it more.

Thanks again,
Keith
 
Old 02-03-2012, 07:36 AM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by keithostertag View Post
I had tried %p as a specifier but didn't know to "cast the pointers to a void pointer first". Don't yet know what "cast" means, I'm teaching myself via books and haven't come across that yet. Could you explain about casting or point to a web page describing its proper use?
Casting is simply converting one type to another. For example:

Code:
float x = 5.5546;
printf("%i\n", (int) x);
prints 5. "(int)" converts the following value to an integer (note that casting floats to integers rounds down instead of to the nearest integer).

When you cast a pointer to another pointer type, C will treat the data pointed to by the pointer as if it was the new type. It doesn't cast the data, it just takes it and reads it as if it was another type, which usually leads to useless garbage. There are some interesting uses for it, though. A void pointer is a pointer to no specific type, and it's actaully invalid to dereference it (with the "*" operator).
 
Old 02-03-2012, 08:33 AM   #5
keithostertag
Member
 
Registered: Jul 2011
Location: Harrisburg, PA
Posts: 92

Original Poster
Rep: Reputation: Disabled
Thank you, that is very helpful :-)

I need some time to mull it over (maybe this weekend). I'm sure you will see more question from me in the near future...

Keith Ostertag
 
Old 02-03-2012, 10:03 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Here's a decent reference on the C lib:
http://www.cplusplus.com/reference/clibrary/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Why printf always print at the end dspjm Programming 6 10-02-2011 06:51 PM
Sed - Replace all spaces at beginning of line with the number 1. felix001 Programming 12 06-21-2010 12:22 PM
[SOLVED] Convert string to number without 0 in the beginning nadinnne Linux - Newbie 11 06-08-2010 08:27 AM
Socket programming in C...print clients IP and queue number... niteshadw Programming 6 02-08-2010 03:07 PM
Beginning to print iqbala Linux - Newbie 1 01-11-2004 05:16 PM


All times are GMT -5. The time now is 08:48 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration