By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,708 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,708 IT Pros & Developers. It's quick & easy.

help me understand this char array please....

P: n/a
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"),now
after copying should str not be actually "hailo"?Because 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,generally the name of the array
means the value of first element in array.i.e,suppose 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
Share this Question
Share on Google+
3 Replies


P: n/a
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"),now
after copying should str not be actually "hailo"?Because 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,generally the name of the array
means the value of first element in array.i.e,suppose 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

P: n/a
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

P: n/a
On 22 Feb 2005 21:26:52 -0800, in comp.lang.c , s_**********@rediffmail.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 discussion thread is closed

Replies have been disabled for this discussion.