Click here to Skip to main content
15,039,149 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the difference between these two--

CASE1:
INPUT:
C++
#include <stdio.h>
int main()
{
    char buffer[25];
    int a = 10, b = 20, c;
    c = a + b;
    sprintf( buffer, "Sum of %d and %d is %d", a, b, c);
 
    printf("%s", buffer);
 
    return 0;
}

OUTPUT TO CASE 1:
Sum of 10 and 20 is 30

CASE 2:
INPUT:

C++
#include <stdio.h>
int main()
{
    char *buffer;
    int a = 10, b = 20, c;
    c = a + b;
    sprintf( buffer, "Sum of %d and %d is %d", a, b, c);
 
   
    printf("%s", buffer);
 
    return 0;
}


OUTPUT:
No output, only black color screen .....

What I have tried:

As writing a string is equivalent to the pointer to the first character of the string , then the output to the both of them should be equal i.e Sum of 10 and 20 is 30.
Why they are different?
I did not understand.......
Posted
Updated 28-Jun-21 21:26pm

   
Comments
Patrice T 29-Jun-21 5:18am
   
+5
CPallini 29-Jun-21 7:28am
   
Thank you!
C
char buffer[25];
Declares a variable called buffer, and allocates it 25 bytes of stack space.
C
char *buffer;
declares a variable called buffer and allocates it 4 bytes of stack space. If it has a value, it is zero.
So when you use it:
C
sprintf( buffer, "Sum of %d and %d is %d", a, b, c);

The first one passes the address of the first element of the array to the sprintf function (because the name of an array is a pointer to the first element by definition in the C language specification).
The second one at best passes a pointer to "zero memory" and at worst a random value as the pointer.
And your app crashes.

Now, if you'd done the second one like this:
C
char *buffer = (char*) malloc(25);
It would have worked.
   
Comments
_-_-_-me 29-Jun-21 1:28am
   
okay, thank you very much!
OriginalGriff 29-Jun-21 1:58am
   
You're welcome, but ... you need to start learning this stuff properly.

What it looks like from your questions at the moment is that you are guessing what to do and trying to make sense of it afterwards - and that's a really bad idea.
Are you on a course, or working from a book? Or - gawd help you - following YouTube tutorials?
_-_-_-me 29-Jun-21 2:47am
   
If I am going in a wrong way , please give a suggestion to me to go in a good way and efficient way.....
I did not find good courses of data and algo through c. But I found good courses on data structures and algorithms through java,c++,python ..
I dont know any of these languages
I am learning course on ds and algo through c from udemy .
I am solving questions from leetcode.
When solving question , if I did not get the approach of doing that , I am referring geeks for geeks.. and referring that concept in deep ...
Is this the bad approach ?
What should I do?
I came to know that it is better to learn ds and algorithms through c++,java,python etc...
I am in first year.
In first year of my college, the syllabus is c language and data structures through c (basic) .
So, I am learning ds and algo through c.
After one month when I go to second year , the syllabus is java and data structures and algorithms through java (advanced).
I did not read any book for c language and data structures through c ..
If I start now, I would not be able to complete in one month.
So , shall I take a good course or start reading a book of ds and algo through java when it get started?
If I am going in wrong way , please say the correct way or correct approach.
Should I take a course or read a book for ds and algo through java
There were some good courses ds and algo through java (6 months) which starts after two months and so many students like 50,000 enrolled until now.
It gives worksheets and gives us our ranking among the students enrolled ...
Shall I take this course or read a book? or both ?
Thank you !!!!
OriginalGriff 29-Jun-21 3:12am
   
C is a very old language, and to be honest you are unlikely to meet it in the real world these days - it's still used, but in specialist code normally for embedded hardware with tiny processors and very little RAM.
Nobody is writing "user centric" code with C anymore as there are far quicker and easier (to develop in) languages around now. (C#, Java, for example) or more flexible (C#, C++ for example).

I'd suggest that for your C course, study the materials you are given, and try to get as good a grade on the exercises without worrying too much about how it all works - since you almost certainly will never meet it again once you are out of college! It's very limited in what data structures it provides, and that makes it a lot, lot harder to write good "modern" applications than a newer object oriented language would.

Java though - that's "the language" for Android development, just as C++ is for Linux, Objective C is for Apple, and C# is for Windows (along with C++) so I'd concentrate more on that when you get to it.
_-_-_-me 29-Jun-21 4:21am
   
Okay , thank you !
KarstenK 29-Jun-21 2:03am
   
you missed the free() :-O
OriginalGriff 29-Jun-21 2:33am
   
That short an app, it doesn't matter - it'll be released when the process ends anyway.
But yes, it's good practice to always release memory.
CPallini 29-Jun-21 3:23am
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900