Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

explanation for the following code?

Posted on 2009-12-19
8
Medium Priority
?
199 Views
Last Modified: 2012-05-08
actually str will have address of abc, str1 will have address of ABC,
hope my view is correct?

while concatinating how it performs ?
and gives output as abcABC

i want to know whether value of both pointers are concatinated?
i am having doubt here!!!

char *str="abc";
char *str1="ABC";
char *str2;
str2=strcat(str,str1);
printf("%s",str2);

Open in new window

0
Comment
Question by:ganeshkumar_cse
  • 3
  • 3
  • 2
8 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 26087552
str does not have sufficient space to hold "abcABC" so the result is undefined
0
 
LVL 3

Expert Comment

by:mnh
ID: 26087554
Hi,

You cannot safely concatenate a new string to a constant string.
Your declarations point to constant strings ("abc" and "ABC").
The correct way is to declare str as a string array, like this:

   char str[100] = "abc";

This makes sure you reserve 100 bytes for the concatenated string, and don't overwrite illegal memory.

Regards,
-- mnh
0
 

Author Comment

by:ganeshkumar_cse
ID: 26087623
guys actually i want to know internal function of the code //str2=strcat(str,str1);

what it is doing?  is it concatinatin two address or strings?



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Accepted Solution

by:
mnh earned 500 total points
ID: 26087634
strcat concatenates the contents of str1 to the end of str, not the addresses.
So if str1 points to "123" and str contains "abc", the result will be "abc123".
0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 500 total points
ID: 26087649
the result will be undefined for the two reasons mentioned
if str had been declared as
char str[100] = "abc";
then the result would be "abcABC"
0
 

Author Comment

by:ganeshkumar_cse
ID: 26087717
Hi ozo!!
no i actually compiled that code

it gave me the output as i said earlier!! u guys can check and ping me if  i am wrong!!
0
 
LVL 3

Expert Comment

by:mnh
ID: 26087731
You got the result by chance. The program is not correct as you wrote it. Trust us on this.
Your code will crash most of the time, since you are overwriting memory that doesn't belong to your program.
0
 
LVL 85

Expert Comment

by:ozo
ID: 26087738
No one said that it would not give that output.
undefined behavior means that the C standard imposes no requirements on the result
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question