once defined ,string cannot be initialized to other characters? [duplicate] - c

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
C - why is strcpy() necessary
(4 answers)
Closed 8 years ago.
Using pointers string can be initialized to other characters but when once string has been defined it cannot be initialized to other characters.What is the reason behind it??
int main()
{
char str1[]="hello";
char *p="hello";
str1="bye";/*error*/
p="bye";/*works*/
}

You've defined str1 as an array, and arrays aren't assignable.
You can, however, copy other data into the array, for example:
char str1[] = "hello";
strcpy(str1, "bye");

Arrays are arrays and pointers are pointers. Defining arrays gives the pointer to the allocated array, that is a constant pointer to the location where the array space hass been reserved. That is a concrete address in the lifo stack. So, str1 is a constant pointer value and you cannot change it. You cannot set the value of the address of a different constant string.
Defining pointers, as char*p, gives you a variable value of an address. And so, you can change de value of the variable p.

To change the characters in an array such as what you are doing you have to use a function such as strcpy or do it index by index.
str1[0] = 'p';
will print out pello
What you are trying to do is not supported by the C language.

Related

Why can't I change a string by index when it is declared by pointer notation? [duplicate]

This question already has an answer here:
Why can I not modify a string literal in c?
(1 answer)
Closed 4 months ago.
for example:
char stringer[]="hello";
stringer[2]='A';
The above works to change 'l' to 'A'. But if I do the following:
char *stringer="hello";
stringer[2]='A';
This doesn't work, is there a reason for this?
As answered by Some programmer dude in comments:
Literal strings are really non-modifiable arrays of characters. With char *stringer="hello"; you make stringer point to the first character of such an array. Attempting to modify its contents leads to undefined behavior.
That's why you should always use const char * when pointing to literal strings.
Strings can be modified when using pointer, if the pointer is pointing to something you're allowed to modify. For example
char stringer[] = "hello";
char *pointer = stringer;
pointer[2] = 'A';

how to point a pointer to string without declaring string directly [duplicate]

This question already has answers here:
What is the difference between char s[] and char *s?
(14 answers)
Closed 6 years ago.
#include <stdio.h>
int main(){
char *ch = "Hello, World";//character pointer pointing to a string without declaration.
printf("%s", ch);//printing the string without <i>dereferencing</i> it.
}
I saw an example in a book, with code as given above. I don't understand how a character pointer points to a string without declaring a string first.Also there is no dereference operator used to print the string.
As stated very well by #tryurbest, Why it works with pointers:
When you say char * ch in C, you are allocating a pointer in the memory. When you write char *ch = "Hello, World";, you are creating a string literal in memory and making the pointer point to it. When you create another string literal "new string" and assign it to ch, all you are doing is changing where the pointer points.
On the other hand, for added information, Why it doesn't work with arrays:
When you say char ch1 [] = "Hello, World", you are creating a string literal and putting it in the array during its definition. It is ok to not give a size, as the array calculates it and appends a '\0' to it. You cannot reassign anything to that array without resizing it.
Hope this helps to clarify some things.
"Hello, World" is a string literal or read only memory store in data block. A variable ch is pointer to char, which is initialized with the location of the first character.
That's simply how printf works with a const char* argument. (Your pointer actually points to const data.)
Starting at the memory location denoted by the pointer, it prints character by character until a NUL terminator is reached.
The C standard library string functions all work in this way. Simply put it's how the language models strings.

How to declare strings in C [duplicate]

This question already has answers here:
Memory Allocation char* and char[]
(3 answers)
Closed 3 years ago.
Can anyone explain me what is a difference between these lines of code
char *p = "String";
char p2[] = "String";
char p3[7] = "String";
In what case should I use each of the above ?
This link should satisfy your curiosity.
Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.
But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.
Strings in C are represented as arrays of characters.
char *p = "String";
You are declaring a pointer that points to a string stored some where in your program (modifying this string is undefined behavior) according to the C programming language 2 ed.
char p2[] = "String";
You are declaring an array of char initialized with the string "String" leaving to the compiler the job to count the size of the array.
char p3[5] = "String";
You are declaring an array of size 5 and initializing it with "String". This is an error be cause "String" don't fit in 5 elements.
char p3[7] = "String"; is the correct declaration ('\0' is the terminating character in c strings).
http://c-faq.com/~scs/cclass/notes/sx8.html
You shouldn't use the third one because its wrong. "String" takes 7 bytes, not 5.
The first one is a pointer (can be reassigned to a different address), the other two are declared as arrays, and cannot be reassigned to different memory locations (but their content may change, use const to avoid that).
char *p = "String"; means pointer to a string type variable.
char p3[5] = "String"; means you are pre-defining the size of the array to consist of no more than 5 elements. Note that,for strings the null "\0" is also considered as an element.So,this statement would give an error since the number of elements is 7 so it should be:
char p3[7]= "String";

Declaration of strings in C [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is it possible to modify a string of char in C?
What is the difference between char s[] and char *s in C?
There is something I don't understand about strings and pointers in C.
Suppose I have this declaration:
char str[] = "abc";
Then, if I attempt to modify it this way:
str[0] = 'b';
It will work.
But if I declare the string as a pointer to a char
char* str = "abc"
The attempt above will cause an access violation.
What I'm trying to understand is what exactly is the difference.
Thanks in advance
In The later example the compiler puts the string in the read only data section, so u can't modify it. But in earlier example you are declaring array of character which resides in stack hence you can modify content of this array.

C strings pointer vs. arrays [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What is the difference between char s[] and char *s in C?
Why is:
char *ptr = "Hello!"
different than:
char ptr[] = "Hello!"
Specifically, I don't see why you can use (*ptr)++ to change the value of 'H' in the array, but not the pointer.
Thanks!
You can (in general) use the expression (*ptr)++ to change the value that ptr points to when ptr is a pointer and not an array (ie., if ptr is declared as char* ptr).
However, in your first example:
char *ptr = "Hello!"
ptr is pointing to a literal string, and literal strings are not permitted to be modified (they may actually be stored in memory area which are not writable, such as ROM or memory pages marked as read-only).
In your second example,
char ptr[] = "Hello!";
The array is declared and the initialization actually copies the data in the string literal into the allocated array memory. That array memory is modifiable, so (*ptr)++ works.
Note: for your second declaration, the ptr identifier itself is an array identifier, not a pointer and is not an 'lvalue' so it can't be modified (even though it converts readily to a pointer in most situations). For example, the expression ++ptr would be invalid. I think this is the point that some other answers are trying to make.
When pointing to a string literal, you should not declare the chars to be modifiable, and some compilers will warn you for this:
char *ptr = "Hello!" /* WRONG, missing const! */
The reason is as noted by others that string literals may be stored in an immutable part of the program's memory.
The correct "annotation" for you is to make sure you have a pointer to constant char:
const char *ptr = "Hello!"
And now you see directly that you can't modify the text stored at the pointer.
Arrays automatically allocate space and they can't be relocated or resized while pointers are explicitly assigned to point to allocated space and can be relocated.
Array names are read only!
If You use a string literal "Hello!", the literal itself becomes an array of 7 characters and gets stored somewhere in a data memory. That memory may be read only.
The statement
char *ptr = "Hello!";
defines a pointer to char and initializes it, by storing the address of the beginning of the literal (that array of 7 characters mentioned earlier) in it. Changing contents of the memory pointed to by ptr is illegal.
The statement
char ptr[] = "Hello!";
defines a char array (char ptr[7]) and initializes it, by copying characters from the literal to the array. The array can be modified.
in C strings are arrays of characters.
A pointer is a variable that contains the memory location of another variable.
An array is a set of ordered data items.
when you put (*ptr)++ you are getting Segmentation Fault with the pointer.
Maybe you are adding 1 to the whole string (with the pointer), instead of adding 1 to the first character of the variable (with the array).

Resources