C snipet on char Pointers


 
Thread Tools Search this Thread
Top Forums Programming C snipet on char Pointers
# 1  
Old 07-26-2005
C snipet on char Pointers

I have small Dout. please respond.


char *p;

p=" I am a Very good boy";


Is this valid or not?

If not valid why is it so?

It is not giving any compilation error.

Thanks

Vinod
# 2  
Old 07-26-2005
It is syntactically valid, the compiler doesn't complain because the syntax is correct in that code, but... wha't really happens there:

char *p; only allocates the sufficient memory to hold the address of the pointer but not the rest elements of the "array". What this really means?, you have a char type pointer with 4 bytes allocated but a string with more than 15 characters.

As result you will have a run time error commonly known as memory fault access, the application receives a SIGSEGV (typically Memory fault or Segmentation fault message on you shell) signal and terminates instantly.

To solve this, you should allocate memory for your string with malloc() or statically initialize the pointer doing char *p=" I am a Very good boy";

using malloc() it maybe shows like this:

char *p;

p = (char *) malloc( (size_t) 20);

sprintf(p, "%s", "I'm a very good boy");


Best regards!

Last edited by infierno; 07-26-2005 at 10:40 AM..
# 3  
Old 07-26-2005
Quote:
Originally Posted by infierno
char *p; only allocates the sufficient memory to hold the address of the pointer but not the rest elements of the "array". What this really means?, you have a char type pointer with 4 bytes allocated but a string with more than 15 characters.
actually, this's not what happens.
a pointer is in most cases is in size of a CPU register, on 32-bits machines, it's usually 32-bits, ie 4-bytes. this 4 bytes contains not data, but a value that is a memory address where the data is held. compiler will not complain, and it's very valid. what happens is, p will get the beginning address of the constant data (usually stored in .rodata -stands for read only data- section, but may differ with the contents of linkscript) in a memory location that should behave as ROM.
# 4  
Old 07-26-2005
Quote:
Originally Posted by fdarkangel
actually, this's not what happens.
a pointer is in most cases is in size of a CPU register, on 32-bits machines, it's usually 32-bits, ie 4-bytes. this 4 bytes contains not data, but a value that is a memory address where the data is held. compiler will not complain, and it's very valid. what happens is, p will get the beginning address of the constant data (usually stored in .rodata -stands for read only data- section, but may differ with the contents of linkscript) in a memory location that should behave as ROM.
yeap... you're right,
subl $24,%esp
movl $.LC0,-4(%ebp) ; .LC0, .rodata section
addl $-8,%esp

... my mistake

best regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

UNIX Script - snipet meaning?

What would the below code snippet mean? my ($_configParam, $_paramValue) = split(/\s*=\s*/, $_, 2); $configParamHash{$_configParam} = $_paramValue; (2 Replies)
Discussion started by: MaKha
2 Replies

2. Programming

Invalid conversion from char* to char

Pointers are seeming to get the best of me and I get that error in my program. Here is the code #include <stdio.h> #include <stdlib.h> #include <string.h> #define REPORTHEADING1 " Employee Pay Hours Gross Tax Net\n" #define REPORTHEADING2 " Name ... (1 Reply)
Discussion started by: Plum
1 Replies

3. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

4. Programming

Need help with the Pointers in C

I have a special character called ô. When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = 'ô'; char *fp; fp="XXô"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

5. Programming

concat const char * with char *

hello everybody! i have aproblem! i dont know how to concatenate const char* with char const char *buffer; char *b; sprintf(b,"result.txt"); strcat(buffer,b); thanx in advance (4 Replies)
Discussion started by: nicos
4 Replies

6. Programming

Adding a single char to a char pointer.

Hello, I'm trying to write a method which will return the extension of a file given the file's name, e.g. test.txt should return txt. I'm using C so am limited to char pointers and arrays. Here is the code as I have it: char* getext(char *file) { char *extension; int i, j;... (5 Replies)
Discussion started by: pallak7
5 Replies

7. Programming

to compare two integer values stored in char pointers

How can I compare two integer values which is stored in char pointers? suppose I have char *a and char *b having values 10 and 20. how can i find the shorter value? (1 Reply)
Discussion started by: naan
1 Replies

8. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

9. Programming

pointers

Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue. void InsertFirst (tList *L, int val) { tElemPtr new; if((new = malloc(sizeof(tElemPtr))) == NULL) Error(); new->data = val; new->ptr = L->frst; L->frst = new;... (2 Replies)
Discussion started by: Milla
2 Replies

10. Programming

pointers

is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies
Login or Register to Ask a Question