473,765 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help me understand this char array please....

Dear all,
I encountered the following piece of program:
#include <stdio.h>
void strprint(char *str);
void main()
{
char *spr="hello";
strprint(spr);

}
void strprint(char *str)
{
char *ptr="hai";
str=ptr;
printf("%s",str );
}

This program when compiled with MSVC++ compiler prints the output as
"hai".

I have a few doubts regarding this program:

1)Whats the statement str=ptr meaning?

2)In the code above,str contains 5 chars ("hello" is passed from
main),and ptr is getting copied to it which has 3 chars("hai"),no w
after copying should str not be actually "hailo"?Bec ause only first 3
chars should be replaced with ptr leaving the rest 2 chars?Can someone
explain me why its not so in the output?

3)AFAIK,when we talk about char arrays,generall y the name of the array
means the value of first element in array.i.e,suppo se i declare char
a[10],when i say a,it refers to a[0].Having this inference,I suppose
this statement str=ptr is supposed to copy only the first character.

4)But I am not getting how it has copied entire array into another
array with this statement?

5)How exactly in a char array,char a[10], 'a' differ from a[0]?Why I
am asking this is,I am under the impression that both are same with
respect to char arrays.But my colleague argues thats not the case.

It will be helpful if some one throws some light on this.
Looking farward to all your replys and advanced thanks for the same,
Regards,
s.subbarayan
Nov 14 '05 #1
3 1989
Mac
On Tue, 22 Feb 2005 21:26:52 -0800, s.subbarayan wrote:
Dear all,
I encountered the following piece of program:
#include <stdio.h>
void strprint(char *str);
This is a bad name for a function, since it starts with str. Call it
my_strprint, or something.
void main()
How about:
int main(int argc, char** argv)

main always returns an int. {
char *spr="hello";
strprint(spr);
return 0; }
void strprint(char *str)
{
char *ptr="hai";
str=ptr;
printf("%s",str );
}

This function makes no sense. The value of the variable str is completely
ignored by the function and doesn't influence the functions behavior at
all.
This program when compiled with MSVC++ compiler prints the output as
"hai".
The compiler is generally irrelevant in this newsgroup, as long as it is a
C compiler.
I have a few doubts regarding this program:

1)Whats the statement str=ptr meaning?
It means assign the value of ptr to str. What did you think it meant?
2)In the code above,str contains 5 chars ("hello" is passed from
main),and ptr is getting copied to it which has 3 chars("hai"),no w
after copying should str not be actually "hailo"?Bec ause only first 3
chars should be replaced with ptr leaving the rest 2 chars?Can someone
explain me why its not so in the output?
The assignment operator (=) does not do what you think. It just takes the
value of ptr, and stores it in str. That value happens to be a pointer to
a char which happens to be the first character in a c string.

3)AFAIK,when we talk about char arrays,generall y the name of the array
means the value of first element in array.i.e,suppo se i declare char
a[10],when i say a,it refers to a[0].
No. You can say that 'a' is the same as '&(a[0])', but it is NOT the same
as a[0]. The type of 'a' is "array of 10 chars" and the type of a[0] is
"char".
Having this inference,I suppose
this statement str=ptr is supposed to copy only the first character.

No. I've already explained this above.
4)But I am not getting how it has copied entire array into another
array with this statement?

No array was copied. The variables str and ptr are pointer types, which
means they hold addresses. When you say str=ptr, you are throwing away the
address currently stored in str and replacing it with the address stored
in ptr.
5)How exactly in a char array,char a[10], 'a' differ from a[0]?Why I
am asking this is,I am under the impression that both are same with
respect to char arrays.But my colleague argues thats not the case.

As I said above, 'a' has type "array of 10 chars," and 'a[0]' has type
"char".
It will be helpful if some one throws some light on this.
Looking farward to all your replys and advanced thanks for the same,
Regards,
s.subbarayan


Your knowledge of C is too basic for this group. You should read at least
one C book before posting questions here.

Please do not post any questions until you have a rudimentary
understanding of the basics of the language, or at least have carefully
read over the FAQ list for this newsgroup:

http://docs.mandragor.org/files/Prog...C-faq/top.html

--Mac

Nov 14 '05 #2
Mac wrote:
<snip Yet Another Clueless Program, + Mac's response>
Your knowledge of C is too basic for this group.
I think I have to disagree with this.
You should read at least
one C book before posting questions here.


But this is certainly wise advice. He would do better to learn by
other people's mistakes than to make all the same mistakes for
himself.
Nov 14 '05 #3
On 22 Feb 2005 21:26:52 -0800, in comp.lang.c , s_**********@re diffmail.com
(s.subbarayan) wrote:
char *ptr="hai";
this line initialises ptr to point to "hai".
str=ptr;
this line throws away the value of str (a pointer to "hello") and replaces
it with the value of ptr (a pointer to "hai"). Note its does NOT copy
anything. It assigns the value of ptr to str. Not what it points to, its
value.
printf("%s",str );
so this line prints out "hai".

This is a very stupid programme, presumably intended to teach you
something. I've no idea what - possibly that the = operator doesn't copy
things, possibly that C's char pointers are not the same as C++ strings.
Possibly nothing at all.
4)But I am not getting how it has copied entire array into another
array with this statement?


in the above code, you do not have any arrays.

char* str = "hello";
creates a pointer to a string literal. To create an array you would use
char str[6] = "hello";
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2760
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the hope that someone could help me out. As said in an earlier post I am new to c++, this is alot harder to do than VB. I am for the most part self taught with what I already know and looking to learn more. The book I am using to teach myself from...
1
2200
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done -seach for a specific flight number in queue ( didn't get there yet) -move a flight number one one position in the queue to another ( didn't get there yet) this is what i have so far. it runs but something is wrong and i don't know what it is.
7
7417
by: tuchka | last post by:
Hi, guys! I am very new here and just started to learn C. I have previous java exp. however. I'm abs. stuck on pointers and i'm unable comprehend algorithm of simple program that reverses chars in string no matter how long I'm staring at it. This is the method(I printed out some lines for better understanding(by me)):
3
1439
by: dj | last post by:
I've read section 6 of the FAQ, but still am a bit confused... Please refer to the comments in the following code for my questions: ----------------------------------------------------------- #include <stdlib.h> #include <stdio.h> #include <memory.h> #define L_DATE 8
28
2199
by: Bailey.W87 | last post by:
my professor give me this assignment. Sort the R's B's and W's in an array. for example, the user enter: R B W W B B R W W R R W R B W i need to swap the characters in the array and arrange it into R R R R R W W W W W W B B B B --------------------------------------------------------------------
8
10718
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
33
2868
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio 2005. Everything is standard C (ANSI C ?? I don't know the difference) - but since so many functions...
21
2329
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to solve in this small code, I can solve it on my own program...you don't want to have head-ache :-) So, the problem excatly is, I built an array..just like this one.
8
1879
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the file into a 2d array. I have this to get the file. #include <iostream> #include <string> #include <fstream> using namespace std;
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.