c Questions regarding initialization of language structure

Asked 2 years ago, Updated 2 years ago, 109 views

typedef struct PersonArray {    
    Person people[NUM_USER];
    int total_waiting_time;     
    int total_waiting_people;   
}PersonArray;

PersonArray *PeopleArray;

PeopleArray->total_waiting_time = 0;
PeopleArray->total_waiting_people = 0;

The structure PersonArray set as the global variable.

And to create a PeopleArray with a structure pointer and initialize the contents of it

PeopleArray->total_waitting_time = 0; PeopleArray->total_waiting_people = 0;

I did, but an error mark appears in the -> part, so ';' is required That's what it says.

Did I do the wrong way to initialize the variables in the structure?

How do I initialize the global variable structure?

And

#include <stdio.h>
#include <stdlib.h>=
#define SIZE_QUEUE 100  //the maximum size of the news_queue
#define CAPACITY 6      //the capacity of ZNN.com (per sec)
#define NUM_USER 20     //the number of users
#define NUM_LOOP 100    //the number of loops


//ContentFidelity is one of the types: video, image, text
typedef enum { Video, Image, Text } ContentFidelity;

typedef struct {
    unsigned int requestedBy;
    ContentFidelity fidelity;
} news; // When user 8 requests a video, it outputs the requested news content as (8, video)

typedef struct {
    news queue[SIZE_QUEUE];
    unsigned int front;
    unsigned int rear;
} Queue; // Queue for stacking news content requested by the user

Queue *news_queue;
Content Fidelity current Fidelity = Video; // First content is video

                                         /**/
typeef structure Person { // User structure (content count and latency information)
    int content_num; // Total number of requested content
    int receive_content; // Total number of received content
    int waiting_time; // total delay time
    int content_score; // Total score of received content
}Person;

typeef structure PersonArray { // Structure for user structure (array)
    Person people[NUM_USER];
    int total_waiting_time; // Total latency
    int total_waiting_people; // Number of users who have requested content so far (redundant)
}PersonArray;

PersonArray *PeopleArray;

PeopleArray->total_waiting_time = 0;
PeopleArray->total_waiting_people = 0;
for (int i = 0; i < 20; i++) {
    PeoepleArray->people[i].content_num == 0;
    PeoepleArray->people[i].receive_content == 0;
    PeoepleArray->people[i].waiting_time == 0;
    PeoepleArray->people[i].content_score == 0;
}

From the above code

for (int i = 0; i < 20; i++) {
    PeoepleArray->people[i].content_num == 0;
    PeoepleArray->people[i].receive_content == 0;
    PeoepleArray->people[i].waiting_time == 0;
    PeoepleArray->people[i].content_score == 0;
}

In this part, the for phrase itself should not be marked with an error.

The values in the arrangement of the people structure in the PeopleArray do not use a function separately

Is there any way to initialize them all to zero?

c struct

2022-09-22 19:26

1 Answers

I think we need to understand what a pointer is.

And why don't you understand that a structure is a big data format and a type that breaks it down?

PersonArray *ptrPeopleArray; 

Pointer ptrPeopleArray is the starting address of memory that has the format PersonArray.

This means you must explicitly point to the memory address of the memory. If you don't have anything to point to, you should create a new one and replace it with the address of the one you created.

ptrPeopleArray= (PersonArray*)malloc(sizeof(PersonArray ));
// Or you can create it on a stack with PersonArray l_personArray; ptrPeopleArray = &l_personArray;.

I'll do it like this.


2022-09-22 19:26

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.