C Programming Tutorial 34, Report Card Program pt.2

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

It's because he created the structure local to the function (main) he was accessing it in. Basically, it's created on the stack at compile-time...aka "statically." If he would have created it at run-time ("dynamically"), then he would have had to use a pointer to the structure--and THEN would have had to access the struct member your way. That's basically what he is getting at in his response to your question.

tcbetka
Автор

It's because 'str' is already pointing to the memory where we want the data to be read into. The scanf function wan't an memory address where it can store the data it reads. Arrays are already addresses so we can't put the & in front of it. Anything that's not a pointer or an array like a local int variable would need the & because we need to give the scanf function the address of the memory where we want the read in data to go. I hope that makes more sense to you.

iTzAdamX
Автор

Forgot to add: He would also have to use the -> operator had the struct been created on the heap, instead of on the stack as it was created here. See his tutes on malloc() for a very good explanation of this.

tcbetka
Автор

You only use the -> if the struct is a pointer. Since in the main function, it isn't then I have to use the . operator.

iTzAdamX
Автор

I think if you wrote a book with this same information it would sell very well.

OleguitoSwagbucks
Автор

Why is there no '&' before 'str' in the scanf function when we input the last name?
kindly respond

SahilPuri
Автор

Thanks, got it, definitely makes sense! :)

SahilPuri
Автор

explain me why you do not use rc->mathGrade here in the main for example?

thewhisperinyourears
Автор

what's the purpose of using pointer for char *name?

SahinErbay
Автор

question: why can't you use scanf("%s", rc.name)? Adam says that it's because name doesn't point to anything yet, so you need another variable. I don't get it. Any help please?

dsmith
Автор

I too am wondering about this, can't you just replace "char *name" with "char str[20]" ?

AYearOld
Автор

What is the difference between
int average = ((*rc).scienceGrade + (*rc).historyGrade + (*rc).mathGrade + (*rc).englishGrade)/4;
and
int average = (rc->scienceGrade + rc->historyGrade + rc->mathGrade + rc->englishGrade)/4;
?

MrEkwador
Автор

Hi Adam,

In the main function, is there a reason why we used that specific variable rc as the reportCard object? I always thought the rc in the main function and the rc in the void printReportCard were just arbitruary. Does it have to match the name in the void printReportCard function? I'm a bit confused. Can you clarify? Thanks.

phatl
welcome to shbcf.ru