0

C++: What's the difference between the code as shown below AND simultaneously omitting the keyword struct in the functional declaration and definition -- code seems to work the same both ways ?

#include <iostream>
#include <string>

struct student{
            int age;
            int number;
        };

void printme( struct student m[]);    // if 'struct' is omitted the code works as fine

int main()
{        
    student s[3];
    s[0].age = 10;
    s[0].number = 333;

    printme(s);

    return 0;
}

void printme( struct student m[]){
        printf("George age and number: %d, %d \n", m[0].age, m[0].number);
    }
Ivo Peev
  • 9
  • 1
  • 2

2 Answers2

0

Notice that you are using and not , so here there is no difference, you can use both of these options.

Check this: Passing structs to functions for more.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

This comes from C, where you had to specify struct (or use typedef). In C++, there's no difference as long as you don't use the same id for both a struct and an object. Please, don't do it, but if your really want to, then you'll need to write e.g. struct student for the type and student for the object (which is not necessarily of type student, if you really want to confuse everyone). Basically, C+ coding standards tend to recommend skipping struct and never having the same id for struct and object.

lorro
  • 10,687
  • 23
  • 36