c language + simple question regarding memory addresses and ASCII characters


 
Thread Tools Search this Thread
Top Forums Programming c language + simple question regarding memory addresses and ASCII characters
# 1  
Old 01-24-2008
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
# 2  
Old 01-24-2008
Quote:
Originally Posted by JamesGoh
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?
# 3  
Old 01-24-2008
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 07:18 PM..
# 4  
Old 01-25-2008
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);

# 5  
Old 01-28-2008
Quote:
Originally Posted by shamrock
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 ?
# 6  
Old 01-29-2008
Quote:
Originally Posted by JamesGoh
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';

# 7  
Old 01-29-2008
Shamrock, another question do individual integers, f. point numbers and doubles have mem. addressess as well ?

thanks for your help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Lower ASCII characters.

Hi, I'm writing a BBS telnet program. I'm having issues with it not displaying lower ASCII characters. For example, instead of displaying the "smiley face" character (Ctrl-B), it displays ^B. Is this because i'm using Ncurses? If so, is there any way around this? Thanks. (3 Replies)
Discussion started by: ignatius
3 Replies

2. Shell Programming and Scripting

Removing these non-ASCII characters from a file

Hi, I have many text files which contain some non-ASCII characters. I attach the screenshots of one of the files for people to have a look at. The issue is even after issuing the non-ASCII removal commands one of the characters does not go away. The character that goes away is the black one with a... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

3. Shell Programming and Scripting

Delete characters & find unique IP addresses with port

Hi, I have a file having following content. <sip:9376507346@97.208.31.7:51088 <sip:9907472291@97.208.31.7:51208 <sip:8103742422@97.208.31.7:51024 <sip:9579892841@97.208.31.7:51080 <sip:9370904222@97.208.31.7:51104 <sip:9327665215@97.208.31.7:51104 <sip:9098364262@97.208.31.7:51024... (2 Replies)
Discussion started by: SunilB2011
2 Replies

4. Shell Programming and Scripting

New line characters in Ascii file

I am having a file(1234.txt) downloaded from windows server (in Ascii format).However when i ftp this file to Unix server and try to work with it..i am unable to do anything.When i try to open the file using vi editor the file opens in the following format ... @ @ @ @ @ @ @ @... (4 Replies)
Discussion started by: appu2176
4 Replies

5. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

6. Programming

memory addresses

you have three variables of type char, int and float in continous memory locations. How do you print the contents of each of these.??? Thanks in advance. (0 Replies)
Discussion started by: areef4u
0 Replies

7. Shell Programming and Scripting

Multibyte characters to ASCII

Hello, Is there any UNIX utility/command/executable that will convert mutlibyte characters to standard single byte ASCII characters in a given file? and Is there any UNIX utility/command/executable that will recognize multibyte characters in a given file name? The typical multibyte... (8 Replies)
Discussion started by: jerardfjay
8 Replies

8. HP-UX

Hex characters of ascii file

Hi, Whats the command or how do you display the hexadecimal characters of an ascii file. thanks Bud (2 Replies)
Discussion started by: budrito
2 Replies

9. Programming

stupid question about ascii characters

i know it's out there, but I cannot remember how to check if a given ascii character string contains all digits or not ... any ideas? ie...function("123") --> OK function("NOT_A_NUMBER") --> returns error thanks!! (2 Replies)
Discussion started by: jalburger
2 Replies

10. Programming

memory addresses

where is addresses(what kind of memory) like this one "df605d50". I want to print address of locan variable: printf("&i - %p", &i); and I have &i - df605d50. (0 Replies)
Discussion started by: Paravozzz
0 Replies
Login or Register to Ask a Question