-
char *s="george";
-
printf(" from s %s\n",s);
-
-
s[0]='k';
-
-
printf(" from new s %s\n",s);
-
-
char p[]="john";
-
printf("from p %s\n",p);
-
-
p[2]='k';
-
-
printf(" from new p %s\n",p);
-
-
plss explain me how dis actually works inside the memory?
1)s is just a pointer holding the adress of 'george' ryt?so if i do s[2]='k' y does it NOT change?
4 1166
s has the address of s[0]. The first g of george.
s[2] is a calculation of s + 2 x sizeof(char). The s is not changed but the calculation results in an address of the o of george.
There are two syntaxes for dealing with arrays. s[2] is the "array notation" where 2 refers to the 3rd element of the array (from the starting character go 2 characters to the right). The other notation is the "pointer notation" where you do a calculation s + 2 x sizeof(char) to get to the 3rd element.
I guess, you will not be able to change constant string via pointer.
Line 4 attempts to change "george" to "keorge" by changing s[0] to 'k'.
Line 11 attempts to change "john" to "joke" by changing t[2] to 'k'.
However, both s and t point at string literals. The C Standard says 6.4.5 String literals
...
A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz".
...
In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence.
...
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
That last paragraph warns that lines 4 and 11 provoke undefined behavior. Avoid undefined behavior.
The following is an example of how to modify your code to avoid undefined behavior. This code only tries to modify char array s. Notice how I use const to make the string literals explicitly read-only. - const char *a = "george";
-
const char b[] = "john";
-
char s[99+1];
-
-
strcpy(s,a);
-
printf("a: %s\n, s);
-
s[0] = 'k';
-
printf("a: %s\n, s);
-
-
printf("\n");
-
-
strcpy(s,b);
-
printf("b: %s\n, s);
-
s[2] = 'k';
-
printf("b: %s\n, s);
-
By the way, "It is unspecified whether these arrays are distinct provided their elements have the appropriate values" means that the compiler is allowed to reduce the number of duplicate string literals in a program.
For example, in the following snippet it is permissible (but not required) for the pointer values of s1, s2 and s3 to be identical (that is, for all of them to point to the same string literal). - const char *s1 = "string";
-
const char *s2 = "string";
-
const char *s3 = "string";
-
-
printf("s1 = %p\n", s1);
-
printf("s2 = %p\n", s2);
-
printf("s3 = %p\n", s3);
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Bryan Parkoff |
last post by:
I want to allocate pointer array into memory so pointer array contains
ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks
like below for example.
unsigned char* A = new...
|
by: Brett |
last post by:
Sorry for the rookie question....
If I have a pointer array (for example):
char *colors = {
"blue",
"green"
};
and I want to add yellow to this array later in my code....how would I do
|
by: Min-su KIM |
last post by:
HI~
I need to access the pointer array via DLL.
Original C code is
char * buf;
for (int i = 0; i < 63; i++) {
buf = new char ;
}
API_CALL(buf);
|
by: Potiuper |
last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C?
Given: code is written in ANSI C; I know the exact nature of the strings to be...
|
by: nozone |
last post by:
Hi. I am new to the C language. I need to store the commands from input to a pointer array without any ""declaration. How to create an pointer array by memory allocation? I tried to use the following...
|
by: MyCGal |
last post by:
Hi,
I have this code I wrote to copy any length lines into array of pointers. The problem is after storing the individual lines into the char pointer array, the dispaly() chops off some lines...
|
by: Nosnibor |
last post by:
Need some direction as how to implement a pointer array which will mimic a 2D array. Any suggestions? This is where I am. Do not work, but i am working on it. Please send suggestions.
I think my...
|
by: aruna.mysore |
last post by:
Hi all,
I have a specific problem passing a function pointer array as a
parameter to a function. I am trying to use a function which takes a
function pointer array as an argument. I am too sure...
|
by: Immortal Nephi |
last post by:
I would like to design an object using class. How can this class
contain 10 member functions. Put 10 member functions into member
function pointer array.
One member function uses switch to call...
|
by: pereges |
last post by:
Hi, can some one please tell me why this program is not able to
function properly. I have a array a and i am trying to create a
pointer array b which points to elements less than 40 in a.
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |