c++ - Some problems with struct -


#include <iostream> using namespace std; struct student{     char name[10];     int  grade; };   int main() {     struct student s[10];     student s[0].name = "jack";     cout<<s[0].name; } 

i want create struct type data student arraign. when did this, errors appeared , didn't know why.following errors:

1.error: redefinition of 's' different type: 'student [0]' vs 'struct student [10]'

    student s[0].name = "jack";             ^ 

2.note: previous definition here

 struct student s[10];                 ^ 

3. error: expected ';' @ end of declaration

 student s[0].name = "jack";                 ^                 ; 

  1. char name[10];:
    1. 10 characters short name.
    2. char assumes names not outside ascii or utf-8, , doesn't you're using unicode library.
    3. fixed-sized arrays storing strings not keeping idiomatic c++.
    4. solution: use std::string or std::wstring - , use unicode library!
  2. struct student s[10]
    1. this not idiomatic c++. struct keyword unnecessary. student s[10]; sufficient.
    2. again, avoid fixed-sized arrays unless know using 10 records. use std::vector<student> instead.
    3. you don't initialize array, data-members contain undefined/uninitialized data. use = {0} zero-out memory and/or define student constructor.
  3. student s[0].name = "jack";
    1. this won't compile. think meant put s[0].name = "jack"
    2. the assignment operator = not defined (by default) strings. note struct's member type char whereas string literal const char[n], in reality you're assigning pointer (due array decay) char member. meaningless operation.
  4. your main not return value. use return exit_success; on success. not strictly required, believe it's practice explicitly return value.

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -