473,396 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Output of a program

main()
{
char *ptr1,*ptr2;

ptr1=(char*)malloc(sizeof(ptr1));
gets(ptr1);

ptr2=(char*)malloc(sizeof(ptr2));
gets(ptr2);

printf("%s",ptr1);
printf("%s",ptr2);

}

suppose ptr1= aaa bbb ccc ddd eee fff ggg
&
ptr2=kkk
output is :-aaa bbb ccc & kkk

why its not printing complete the first string

Pls any one help me bcoz i need it Urgent

Thanks
sonu

Mar 8 '06 #1
8 2341
sonu wrote:
main()
{
char *ptr1,*ptr2;

ptr1=(char*)malloc(sizeof(ptr1));
gets(ptr1);

ptr2=(char*)malloc(sizeof(ptr2));
gets(ptr2);

printf("%s",ptr1);
printf("%s",ptr2);

}

suppose ptr1= aaa bbb ccc ddd eee fff ggg
&
ptr2=kkk
output is :-aaa bbb ccc & kkk

why its not printing complete the first string

Pls any one help me bcoz i need it Urgent


You're allocating just enough memory to hold a character pointer, i.e.,
malloc(sizeof(ptr1)) is likely to be something like malloc(4).

If you now enter 'aaa bbb ccc ddd eee fff ggg' and use gets() to read that
into the memory you've allocated, you've over written stuff, and from that
point on, it's not possible to say what the program will do - it's
undefined.

So, you ought to be allocating more memory than you have - try something
like malloc(100), and then use fgets() to read your input lines.

Lastly, main() ought to be int main(void) in your case, you don't need to
cast malloc's return, you should free(ptr1) and ptr2 when you're done with
them, and you should add return 0; [or something like]. You should also
include both stdio.h and stdlib.h if you haven't.
--
==============
Not a pedant
==============
Mar 8 '06 #2
sonu wrote:
main()
int main(void)

is much better style.
{
char *ptr1,*ptr2;

ptr1=(char*)malloc(sizeof(ptr1));
You've just masked a bug. You did not include <stdlib.h>, and have cast
the return value of `malloc`. Without prototype (in <stdlib.h>) the
compiler has to assume `malloc` returns an `int`, which you then force
into a `char *`. *Never* cast the return value of `malloc`.

Aside from that, you allocate the space for `ptr1` the size of the
pointer to `char` on your implementation. Is this really what you
wanted? Probably not, as you then go on and input "aaa bbb ccc ddd eee
fff ggg" which is most likely much bigger.

Congratulations! You've just created a buffer overflow vulnerability.
gets(ptr1);

ptr2=(char*)malloc(sizeof(ptr2));
Exactly the same problems as above. You manage not to overflow your
buffer (assuming sizeof(char *) >= 4, which is reasonable on modern
hosted implementations), as you input "kkk", which is 4 bytes long
(remember that pesky terminating \0).
gets(ptr2);

printf("%s",ptr1);
printf("%s",ptr2);
Not terminating `printf` with `\n` (or doing `fflush(stdout)`) may
result in nothing at all being output (output is line buffered -- no
end of line, no output, maybe).
}

suppose ptr1= aaa bbb ccc ddd eee fff ggg
&
ptr2=kkk
output is :-aaa bbb ccc & kkk

why its not printing complete the first string
See comments above. BTW, I'm surprised you get the output you claim you
do.
Pls any one help me bcoz i need it Urgent


The only urgent things for you right now would be to go back to your C
text book.

Someone may be able/willing to help you further if you specify exactly
what the code above is supposed to achieve.

--
BR, Vladimir

Mar 8 '06 #3
Thanx pemo

Mar 8 '06 #4
"sonu" <sa****************@gmail.com> writes:
main()
{
char *ptr1,*ptr2;

ptr1=(char*)malloc(sizeof(ptr1));
gets(ptr1);

ptr2=(char*)malloc(sizeof(ptr2));
gets(ptr2);

printf("%s",ptr1);
printf("%s",ptr2);

} [...] Pls any one help me bcoz i need it Urgent


A few things not yet mentioned in other responses:

Proper indentation is your friend, even for a small program like this
one. See any decent C textbook, or most of the code posted here, for
examples of proper indentation.

Please don't use silly abbreviations like "Pls" for "Please", or
"bcoz" for "because". They only make it more difficult to read what
you write, especially for readers whose native language isn't English.

And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #5
Banfa
9,065 Expert Mod 8TB
And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)
What is the reason that you should "never ever ever use gets(). Use fgets()"?
Mar 8 '06 #6

Keith Thompson wrote:
And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)


What is the reason that you should "never ever ever use gets(). Use
fgets()"?

Mar 8 '06 #7
Banfa said:

Keith Thompson wrote:
And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)


What is the reason that you should "never ever ever use gets(). Use
fgets()"?


Because you tell fgets how long your buffer is, it can (and does) protect
your buffer against overflow.

You have no way to tell gets how long your buffer is.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 8 '06 #8
"Banfa" <Be***************@hotmail.com> wrote in
news:11**********************@z34g2000cwc.googlegr oups.com:

Keith Thompson wrote:
And finally, never ever ever use gets(). Use fgets() instead.
(You'll need to deal with the '\n' character that fgets() stores in
the string, and with its behavior if the input line is longer than
your buffer.)


What is the reason that you should "never ever ever use gets(). Use
fgets()"?

http://c-faq.com/stdio/getsvsfgets.html

--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

Mar 8 '06 #9

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

Similar topics

3
by: Yuan HOng | last post by:
In my program I have to call an external program and parse its output. For that I use the os.popen2 function, and then read the output stream. But the complexity is that the external program...
0
by: Keith Dick | last post by:
I'm trying to use the free command line C/C++ compiler for .NET and when I try to debug a simple C program that uses printf(), I don't see the output it produces. A console window does open when...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
3
by: Liren Zhao | last post by:
I use some "Console.WriteLine(some strings here)" to display some debug information in my winform program. How can I get the information in my programe or same them in a text file ?
2
by: JezB | last post by:
I'm compiling an old C program within Visual Studio to give me the advantages of debugging within this environment. I'm building it as a Console application using C++ as the language. It compiles...
1
by: noleander | last post by:
Hi. I've got a C++ program written in Visual C++ 2003. The program is trivial, created with the Program-creation wizard: used the .NET "Form" template. The program has a trivial...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.