| | |
Difference between char* and char[]
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Hi. Below are two pieces of code. This first one works fine:
However this second one results in a segmentation fault.
The difference is in the definition of the variable str. Why does one method work fine and the other result in a segmentation fault? When is it best to use a str[] definition and the *str definition?
C Syntax (Toggle Plain Text)
char str[] = "Hello World"; char *b; b = &(str[3]); *b = '3'; printf("%s\n", str);
However this second one results in a segmentation fault.
C Syntax (Toggle Plain Text)
char *str = "Hello World"; char *b; b = &(str[3]); *b = '3'; printf("%s\n", str);
The difference is in the definition of the variable str. Why does one method work fine and the other result in a segmentation fault? When is it best to use a str[] definition and the *str definition?
Last edited by Dark_Knight; Jan 15th, 2008 at 12:13 am.
Live life on the edge ... you take up less room that way.
The first is local data you can edit (since the array is local data).
The second is a local pointer to global, static (constant) data. You are not allowed to modify constant data.
If you have GNU C, you can compile with
The second is a local pointer to global, static (constant) data. You are not allowed to modify constant data.
If you have GNU C, you can compile with
-fwritable-strings to keep the global string from being made constant, but this is not recommended. ![]() |
Similar Threads
- converting from char to ASCII to char (C++)
- Is casting a static array to (char*) in strcpy() dangerous? (C)
- What's the difference? (Java)
- OOP char [8] to char (C++)
- Global char returns numbers instead of text (C++)
- Pointers (C++)
- Sorting arrays of pointers with function? (C)
- Pointers (Part II) (C)
- Need to know the difference (C++)
- The difference between functions and templates? (C++)
Other Threads in the C Forum
- Previous Thread: operation on tape using st driver
- Next Thread: [Error]Invalid combination of Opcode and Operands
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest histogram inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






