473,387 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pointer array

4
Expand|Select|Wrap|Line Numbers
  1. char *s="george";
  2. printf(" from s %s\n",s);
  3.  
  4. s[0]='k';
  5.  
  6. printf(" from new s %s\n",s);
  7.  
  8. char p[]="john";
  9. printf("from p %s\n",p);
  10.  
  11. p[2]='k';
  12.  
  13. printf(" from new p %s\n",p);
  14.  
  15.  

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?
Jul 1 '14 #1
4 1166
weaknessforcats
9,208 Expert Mod 8TB
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.
Jul 1 '14 #2
I guess, you will not be able to change constant string via pointer.
Jul 1 '14 #3
donbock
2,426 Expert 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. const char *a = "george";
  2. const char b[] = "john";
  3. char s[99+1];
  4.  
  5. strcpy(s,a);
  6. printf("a: %s\n, s);
  7. s[0] = 'k';
  8. printf("a: %s\n, s);
  9.  
  10. printf("\n");
  11.  
  12. strcpy(s,b);
  13. printf("b: %s\n, s);
  14. s[2] = 'k';
  15. printf("b: %s\n, s);
  16.  
Jul 2 '14 #4
donbock
2,426 Expert 2GB
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).
Expand|Select|Wrap|Line Numbers
  1. const char *s1 = "string";
  2. const char *s2 = "string";
  3. const char *s3 = "string";
  4.  
  5. printf("s1 = %p\n", s1);
  6. printf("s2 = %p\n", s2);
  7. printf("s3 = %p\n", s3);
Jul 2 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
5
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
2
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);
2
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...
1
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...
1
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...
5
Nosnibor
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...
26
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...
5
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...
13
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. ...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.