The difference between C and C++ for (memory allocation for structures)

Asked 1 years ago, Updated 1 years ago, 59 views

Hello, I'm a reserve who just got discharged from the military while preparing to return to school for the second semester of the second year of computer engineering.

I was curious while looking at the data structure book and writing it according to the list example.

The book has written all examples in C++ to illustrate object orientation

I haven't studied C++ before, and I can't find it even if I ask Google God, so I'm asking you this question!


#include <stdlib.h>

typedef struct nodeRecord {
    int Data;
    struct nodeRecord* Next;
}node;

typedef node* Nptr;

Nptr p = (node*)malloc(sizeof(node));

p->Data = 33;

I have three questions about the above source code.

Nptrp = (node*)malloc(sizeof(node)); When you change the extension from cpp to c Why doesn't the compilation error "No Storage Class or Formatter" appear?

For p->Data = 33;, why does the compilation error "No Storage Class or Formatter" occur, whether c or cpp extension?

In the C compiler, it's over if you just allocate memory, but why do you have to convert it in the C++ compiler?

someone has a fundamental question only basic questions, profound isigejji or

question.

I think it's important to be curious about everything.

I'll google it and look forward to your answer!

Thank you!

struct malloc pointer c c++

2022-09-20 17:23

2 Answers

Number one and two. Did you change the chord like that way? If you turn it like that, there will be an error because there is no main function. If you declare a variable in the main function and approach the variable as shown below, both c and c++ run without any problems.

#include <stdlib.h>

typedef struct nodeRecord {
    int Data;
    struct nodeRecord* Next;
}node;

typedef node* Nptr;


int main()
{
    Nptr p = (node*)malloc(sizeof(node));

    p->Data = 33;

    return 0;
}

3. From a design point of view, c is characterized by simplicity, and c++ language is characterized by minimizing execution time errors. Therefore, c++ considers the implicit type transformation to be an error in the default compilation option. Therefore, if you don't change the compilation option in particular, you have to change the type. In the case of c, we simply accept the type transformation. Rather than liking either side, there are advantages and disadvantages depending on the view.

Let me give you one piece of advice. I hope you don't feel bad. As in the question, I think it's wrong to look at the data structure in c++ without learning c++. C doesn't take much time to learn because of its low grammar, but C++ has many times more to learn than C. c seems to have learned, so I recommend you to study the data structure book in c (for example, Yoon Sung-woo's data structure), or study the data structure in c++ after reading at least one c++ grammar book.

Cheer up.


2022-09-20 17:23

I can't believe I was doing that without the main().

was in his right mind ... (• Extract) ;;

Seeing me make a mistake that even a first-time learner wouldn't do

I still feel that I'm not good at writing code.

Thank you very much for telling me not to study with C++ but to look at it as a book in C for data structure study.

I don't have anyone around me who can give me some advice

Your sincere words are precious

Thank you for reading the long article and for your long reply and sincere advice.

Have a good day! ^

^


2022-09-20 17:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.