473,471 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C pointers with char array

4 New Member
Expand|Select|Wrap|Line Numbers
  1. void func(char *s)
  2. {
  3. s+=1;
  4. }
  5. void main()
  6. {
  7. char s[]="neymar messi";
  8. func(s);
  9. printf("%s",s);
  10. }
  11.  
Above program shud have gvn me eymar messi ryt?
but its showing 's' as it is...y?

But in this case the output is nekmar messi....
Expand|Select|Wrap|Line Numbers
  1. void func(char *s)
  2. {
  3. s[2]='k';
  4. }
  5. void main()
  6. {
  7. char s[]="nekmar messi";
  8. func(s);
  9. printf("%s",s);
  10. }
  11.  
Jun 30 '14 #1
4 1148
donbock
2,426 Recognized Expert Top Contributor
C uses a Call-by-Value programming model.

You reuse variables name s inside functions func and main, and that makes the explanation more confusing. I will edit your first snippet to eliminate the confusion.
Expand|Select|Wrap|Line Numbers
  1. void func(char *t)
  2. {
  3. t+=1;
  4. }
  5. void main()
  6. {
  7. char s[]="neymar messi";
  8. func(s);
  9. printf("%s",s);
  10. }
main passes s to func, which has the effect of copying the value of main's s into func's t. func then increments t, but that has no effect on main's s, so line 9 prints the original string.

Now let's look at the second snippet...
Expand|Select|Wrap|Line Numbers
  1. void func(char *t)
  2. {
  3. t[2]='k';
  4. }
  5. void main()
  6. {
  7. char s[]="nekmar messi";
  8. func(s);
  9. printf("%s",s);
  10. }
main passes s to func, which has the effect of copying the value of main's s into func's t. func then writes 'k' into the third character of the string pointed to by t. t has the same value as s, so it points at the same string that s points at; thus 'k' is written to the third character of the string pointed to by s, but that character is already 'k', so this write has no effect.

By the way, in the second snippet you are overwriting a string literal. The C Standard says doing so has unpredictable results. I can't remember if this is formally labeled undefined behavior or implementation defined behavior, but either way it is ill-advised.

By the way, main should always return int.

By the way, in general, a printf format string should end with newline. That is, line 9 of both snippets should be:
Expand|Select|Wrap|Line Numbers
  1. printf("%s\n",s);
Jun 30 '14 #2
kudos
127 Recognized Expert New Member
Here you probably should have a peak at something I wrote about pointers (http://bytes.com/topic/c/answers/956...16-bit-pointer).

You start by sending a pointer s into the method func. Try to write printf("%d\n",s); What is the output? Probably a weird value, because that is the address to the pointer (made by the compiler). Now, what you do, it to add 1 to the pointer value, i.e., you look at what string starts at the same memory location +1.

Let me illustrate:

Expand|Select|Wrap|Line Numbers
  1. char c[] = "hello, how are you";
  2. char *d = c;
  3. printf("%d\n",d); // where the pointer d points (in memory)
  4. printf("%s\n",d); // the content of the string placed at that address
  5. d+=1; // add 1 to where it points in memory
  6. printf("%d\n",d); // what is the address now?
  7. printf("%s\n",d); // and what is the content now???
  8.  
Jun 30 '14 #3
donbock
2,426 Recognized Expert Top Contributor
Use the "%p" format specifier to print the value of a pointer; otherwise you are relying on undefined behavior.
Jun 30 '14 #4
333471
4 New Member
im still confused...in both cases it was call by value...so den it shud nt reflect the changes...but in the second case its reflecting it(the 's' was neymar messi and not 'nekmar messi')
Jul 1 '14 #5

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

Similar topics

11
by: Pontus F | last post by:
Hi I am learning C++ and I'm still trying to get a grip of pointers and other C/C++ concepts. I would appreciate if somebody could explain what's wrong with this code: ---begin code block--- ...
6
by: A_StClaire_ | last post by:
hi, wrote the following program to learn a bit about auto pointers. weird thing is, the char array 'name' with size 'max_size' displays a bunch of messed up symbols after the "John Smith" if...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
14
by: Rick | last post by:
Hi, This is probably piece of cake for you C gurus but it is giving me a headache. I'm still not really used with pointers and chars so many things are going wrong. Practicing makes wonders but...
7
by: arkobose | last post by:
hey everyone! i have this little problem. consider the following declaration: char *array = {"wilson", "string of any size", "etc", "input"}; this is a common data structure used to store...
2
by: Amrit Kohli | last post by:
Hello. I have the following code, to do a simple operation by copying the elements of a vector of strings into an array of char pointers. However, when I run this code, the first element in the...
5
by: eagle_jyjh | last post by:
For example: the msg = temp_buf; is alwawys ok? //test_msg.cpp struct msg_head { char a01;
13
by: Superman859 | last post by:
Hello everyone. Heads up - c++ syntax is killing me. I do quite well in creating a Java program with very few syntax errors, but I get them all over the place in c++. The smallest little things...
4
by: vaiism | last post by:
I am trying to write a string compare function using pointers and dynamic arrays, and am having trouble comparing the individual elements of the array contained with the struct. The code below...
2
by: nagesh0280 | last post by:
Hi experts, I'm from a Verilog HDL background and trying to learn C. There are a lot of similarities between Verilog and C but the concept of char arrays and strings has me confused. I'd...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.