The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
memory addresses areef4u High Level Programming 0 08-04-2006 07:43 AM
Multibyte characters to ASCII jerardfjay Shell Programming and Scripting 8 11-18-2005 01:45 AM
Hex characters of ascii file budrito HP-UX 2 08-10-2005 10:26 PM
stupid question about ascii characters jalburger High Level Programming 2 09-28-2004 03:56 PM
memory addresses Paravozzz High Level Programming 0 10-15-2002 12:17 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-24-2008
Registered User
 

Join Date: Nov 2007
Posts: 89
c language + simple question regarding memory addresses and ASCII characters

Just a simple question (which may seem silly so bear with me) that arose in my mind the other day. Do ASCII characters by themselves (e.g. /n, 0, a) have an actual memory address ?

My question arises, because Im aware that each time I create and initalise a pointer like this for example

Code:
int *ptr = 5;
I always get a null pointer error.

many thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-24-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 704
Quote:
Originally Posted by JamesGoh View Post
Just a simple question (which may seem silly so bear with me) that arose in my mind the other day. Do ASCII characters by themselves (e.g. /n, 0, a) have an actual memory address ?

My question arises, because Im aware that each time I create and initalise a pointer like this for example

Code:
int *ptr = 5;
I always get a null pointer error.

many thanks
Yes they do. Your declaration creates a pointer but does not constrain it to a variable of type int. Does your code compile okay and on what compiler?
Reply With Quote
  #3 (permalink)  
Old 01-24-2008
Registered User
 

Join Date: Nov 2007
Posts: 89
For the code I spoke of in my first post,

Code:
int *ptr=5;
the compiler (I am using gcc btw), produced one warning saying that a pointer from integer was made without a necessary typecast. (shamrock warned me of this, so this gcc output was probably expected).

I tried this

Code:
char *str="helloworld\n";
printf("string value is %s\n",*str)
and found the program compiled fine, but at runtime I got a segmentation fault error. I also found that in the first code segment, if you ignore the compiler warning and run the program straight, you get the same run-time error.

As pointers are meant to be assigned to memory locations (and point to values), with respect to this basic understanding and the fact that ASCII characters have memory addresses, aren't both code segments technically correct ??

Last edited by JamesGoh; 01-24-2008 at 06:18 PM..
Reply With Quote
  #4 (permalink)  
Old 01-25-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 704
Code:
int *ptr=5;
Pointer can only be initialized to zero or null if it does not point to a variable of that type.

Code:
char *str="helloworld\n";
printf("string value is %s\n",*str)
The "%s" conversion specification takes a pointer argument not the actual character that *str points to. So if you want to print the entire string...

Code:
printf("string value is %s\n", str);
and if you want to print the character that *str points to...

Code:
printf("str points to %c\n", *str);
Reply With Quote
  #5 (permalink)  
Old 01-28-2008
Registered User
 

Join Date: Nov 2007
Posts: 89
Quote:
Originally Posted by shamrock View Post
Code:
int *ptr=5;
Pointer can only be initialized to zero or null if it does not point to a variable of that type.
So this is always the case (except for user-defined strings) in real world programming, even though individual ASCII characters (such as 5) have memory addresses ?
Reply With Quote
  #6 (permalink)  
Old 01-29-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 704
Quote:
Originally Posted by JamesGoh View Post
So this is always the case (except for user-defined strings) in real world programming, even though individual ASCII characters (such as 5) have memory addresses ?
Yes ASCII characters have memory addresses though 5 is an integer not an ASCII character. To be intrepreted a character in C it needs to be in single quotes.

Code:
char v = '5';
Reply With Quote
  #7 (permalink)  
Old 01-29-2008
Registered User
 

Join Date: Nov 2007
Posts: 89
Shamrock, another question do individual integers, f. point numbers and doubles have mem. addressess as well ?

thanks for your help
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
None

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 07:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66