| |

VerySource

 Forgot password?
 Register
Search
View: 11|Reply: 4

A few particularly confusing issues, heroes help

[Copy link]

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

Post time: 2020-3-23 16:00:01
| Show all posts |Read mode
The younger brother just learned the C language. I encountered several problems in the recent code and couldn't solve it for two days. I was really depressed. I hope the heroes will help:
#include <stdio.h>
#define DATATYPE1 int
#define MAXSIZE 100


typedef struct
{
  DATATYPE1 datas [MAXSIZE];
  int last;
} SEQUENLIST;

main ()
{
  SEQUENLIST squn1;
  squn1.datas [MAXSIZE] = {0,1,2,3,4}; // Why not write like this
Ranch
}

Why is it not possible to assign squn1.datas in this code? I see the same in the textbook. I always get the following error after assignment
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp (58): error C2059: syntax error: '('
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp (58): error C2143: syntax error: missing ';' before '('
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp (58): error C2143: syntax error: missing ';' before ')'
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp (76): warning C4508: 'main': function should return a value; 'void' return type assumed
Error executing cl.exe.

However, I changed it to squn1.datas [0] = 0; in this way, the assignment of a single element to a single element is fine. It is also why the assignment is not possible. I hope heroes will help.
Reply

Use magic Report

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-7-2 22:30:02
| Show all posts
second question:
Still this code:

#include <stdio.h>
#define DATATYPE1 int
#define MAXSIZE 100


typedef struct
{
  DATATYPE1 datas[MAXSIZE];
  int last;
}SEQUENLIST;

  SEQUENLIST squn1;
  squn1.datas[MAXSIZE]={0,1,2,3,4};//Why not write like this
main()
{
}

Why do you get an error when you put squn1.data outside, and it's okay to put SEQUENLIST squn1 outside
The error is
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(55): error C2143: syntax error: missing';' before'.'
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(55): error C2501:'squn1': missing storage-class or type specifiers
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(55): error C2143: syntax error: missing';' before'.'
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(55): error C2143: syntax error: missing';' before'{'
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(55): error C2447: missing function header (old-style formal list?)
D:\vc\MSDev98\MyProjects\chapter2\sequenlist.cpp(74): warning C4508:'main': function should return a value;'void' return type assumed
An error occurred while executing cl.exe.
Reply

Use magic Report

0

Threads

19

Posts

11

Credits

Newbie

Rank: 1

Credits
11

 China

Post time: 2020-7-3 01:45:01
| Show all posts
squn1.datas[MAXSIZE]={0,1,2,3,4};//Why not write like this

Use {...} to assign values ​​to arrays, only when they are defined, and only one by one when assigning them later.
Reply

Use magic Report

0

Threads

14

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 Thailand

Post time: 2020-7-3 11:00:01
| Show all posts
It can be written as int a[6]={0,1,2,3,4} during initialization;
The general assignment can only be written as a[0]=0, a[1]=1,......
Reply

Use magic Report

1

Threads

3

Posts

4

Credits

Newbie

Rank: 1

Credits
4

 China

 Author| Post time: 2020-7-3 20:45:01
| Show all posts
Thank you two friends upstairs, is there an answer to the second question
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list