CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
What is wrong with the following c program?
char *s1 = "hello";
char *s2 = "world";
char *s3 = strcat(s1, s2);

Please provide me explanations??


Category C Interview Questions
Rating (1.0) By 1 users
Added on 7/17/2006
Views 943
Rate it!
Answers:

Since what is present in memory beyond "United" is not known and we are attching "Front" at the end of "United", thereby overwriting something, which is an unsafe thing to do.



see the declarations You have specified here.
char *s1="hello";
char *s2="world";
Now U self check whether you get the same output hello for
gets(s1);
(or else)
printf("%c",s1);

Definitely No.

Because *s1 is different from s1.

we know &(*s1)=s1.Here You are calling the address of s1 but not the actual *s1.
So try to change it as

strcat(*s1,*s2);



 Posted by: kalavnagarjuna    

Contact kalavnagarjuna  Contact kalavnagarjuna

char *s1="hello";
char *s2="world";
char *s3=strcat(s1, s2); here, we are not allocating the memory to the s3. so givs "Segmentation Fault". And that to we cant not assign the strcat val to s3, as strcat returns a pointer to the resulting dest string, which is s1 here.



 Posted by: Santosh    

Contact Santosh  Contact Santosh

This code will not work. Because the strings "hello" and "world" are stored in data section and their starting address will be assigned to pointers. Since strcat appends the second string at the end of the first string and the address after hello is only readable or may not be in our process address space it gives Segmentation Fault when executed.

Changing this code snippet to the following will work:

char s1[20] = "hello";
char *s2 = " world";
char *s3 = strcat(s1,s2);



 Posted by: Vemula Venugopal    

Contact Vemula Venugopal  Contact Vemula Venugopal


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Between a long pointer and a char pointer , which one consumes more memory? explain

View Answer
What does it mean-

a[i]=i+i

View Answer
How to break cycle in circular single link list?

View Answer
How to improve my c knowledge........

View Answer
How do you write a program which produces its own source code as its output?

View Answer
How to write a C program to find the power of 2 in a normal way and in single step?

View Answer
I want C program code for : Reverse the links of a linked list by traversing only once

Input:
The input consists of the information in each node of the linked list.

Output:
The program displays the information in the linked list after the links are reversed.

Sample Input:
Enter the information in the linked list (Enter -1 to exit): 10
20
30
40
50
-1


Sample Output:
After the links are reversed
Information in the linked list:
50
40
30
20
10




View Answer
Write a program which accepts a filename as a command line argument and coverts all characters in the file to uppercase.



View Answer
Write a program which accepts a filename as a command line argument and reverses the contents of the file( ie first character

becomes the last character of the file and so on)


Input: The program takes the file name whose content should be reversed.

Output: The program reverses the contents of the file.



View Answer
How to type a string without using printf function?

View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/5950/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.78