简体   繁体   English

如何使用指向结构体的指针?

[英]How to work with pointer to pointer to a struct?

I have a bit of a problem. 我有一个问题。 I am barely starting out with C, (comming from a C# background) and I am having problem with double pointers. 我几乎从C(从C#背景开始)入手,而我却遇到了双指针问题。

I have a structure as follows: 我的结构如下:

#ifndef __PROCESSINFO_H
#define __PROCESSINFO_H

struct ProcessInfo
{
   int ProcesId;
   int Priority;
   int ExecutionTime;
   int EllapsedTime;
   char* ProcessName;
}; 

struct ProcessInfo *ProcessInfo_Allocate(int processId, char *processName, int priority, int executionTime);
void ProcessInfo_ToString(struct ProcessInfo *processInfo);
void ProcessInfo_Dispose(struct ProcessInfo *processInfo);

#endif

Implementation: 实现方式:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "processinfo.h"

struct ProcessInfo *ProcessInfo_Allocate(int processId, char *processName, int priority, int executionTime)
{
    struct ProcessInfo *processInfo;
    processInfo = (struct ProcessInfo *)malloc(sizeof(struct ProcessInfo));
    processInfo->ProcessId = processId;
    processInfo->ProcessName = processName;
    processInfo->Priority = priority;
    processInfo->ExecutionTime = executionTime;
    processInfo->EllapsedTime = 0;

    return processInfo;
}

void ProcessInfo_ToString(struct ProcessInfo *processInfo)
{
    printf(" %6i %6i %10i %10i, %25s", processInfo->ProcessId, processInfo->Priority, processInfo->ExecutionTime, processInfo->EllapsedTime, processInfo->ProcessName); 
}

void ProcessInfo_Dispose(struct ProcessInfo *processInfo)
{
    if(processInfo != NULL)
    {
        if(processInfo->ProcessName != NULL)
        {
            free(processInfo->ProcessName);
        }

        free(processInfo);
    }
}

so now I have to manage a whole lot of ProcessInfo instances. 所以现在我必须管理很多ProcessInfo实例。 I wrote another structure which would hold a pointer to a pointer to the ProcessInfo sturcture because i thought that I can increase and decrease it in size as needed (without too much hassle); 我写了另一个结构,该结构包含一个指向ProcessInfo结构的指针,因为我认为我可以根据需要增加或减小它的大小(而不必太麻烦)。

#ifndef __SCHEDULER_H
#define __SCHEDULER_H

struct Scheduler
{
    struct ProcessInfo** Processes;
};

struct Scheduler* Scheduler_Allocate(void);

#endif

So the question is how do I initialize the **Processes member inside the Scheduler_Allocate method? 所以问题是如何在Scheduler_Allocate方法内初始化** Processes成员? How do I add stuff to it? 我如何添加东西?

You don't need a double pointer to increase/decrease the size. 您不需要双指针来增加/减小大小。 Just use a normal pointer and realloc . 只需使用普通的指针并重新realloc

struct ProcessInfo* processes = malloc(sizeof(struct ProcessInfo) * 2);
struct ProcessInfo* processes_tmp;

if (!processes) {
   /* bail */
}

/* do whatever with processes[0] and [1] */

processes_tmp = processes;
processes = realloc(processes, sizeof(struct ProcessInfo) * 5);
if (!processes) {
    free(processes_tmp);
    /* bail */
}

/* processes[0] and [1] are unchanged, and [2] [3] and [4] are now valid */

Then instead of having a ProcessInfo_Allocate , you could create a ProcessInfo_Init that would do most of the same except not allocating the memory: 然后,除了创建ProcessInfo_Allocate ,您可以创建一个ProcessInfo_Init ,除了不分配内存外,该过程将执行大多数相同的操作:

int ProcessInfo_Init(struct ProcessInfo *pi, int processId, char *processName, int priority, int executionTime)
{
    if (!pi) {
        return -1;
    }
    pi->ProcessId = processId;
    pi->ProcessName = processName;
    pi->Priority = priority;
    pi->ExecutionTime = executionTime;
    pi->EllapsedTime = 0;

    return 0;
}
struct Scheduler s;
s.Processes = malloc(sizeof(struct ProcessInfo*) * size);
s.Processes[0] = ProcessInfo_Allocate(...);

// Add more items:
s.Processes = realloc(malloc(sizeof(struct ProcessInfo*) * (size + 1));
s.Processes[size] = ProcessInfo_Allocate(...);
size++;

Also see my example here: 也可以在这里查看我的示例:

Array of C structs C结构数组

size_t size = 10;//or what ever is the number of processes
struct ProcessInfo * process = (struct ProcessInfo *)malloc(size * sizeof(struct ProcessInfo));
if(!process)
    //perhaps stop program? Do something
Processes = &process;
//later on
int i;
for(i = 0; i < size; i++)
{
   printf("Process id =%d",Processes[i]->ProcesId);
   etc
}

Pointer to pointer is initialized as array of pointers. 指向指针的指针被初始化为指针数组。 So call malloc(count * sizeof(ProcessInfo*)) to initialize it. 因此,调用malloc(count * sizeof(ProcessInfo*))进行初始化。 This way you get array of pointers to ProcessInfo. 这样,您将获得指向ProcessInfo的指针数组。 Then call malloc(sizeof(ProcessInfo)) many times to create particular ProcessInfo structures and put pointers to them to the array. 然后多次调用malloc(sizeof(ProcessInfo))创建特定的ProcessInfo结构,并将指向它们的指针放入数组。

Also, user470379 is right that you don't need pointer to pointer just to change number of items in your array. 另外,user470379是对的,您不需要指向指针的指针就可以更改数组中的项数。 But your idea is actually not bad either, you can stay with it if you want. 但是您的想法实际上也不错,可以根据需要坚持下去。

Also, since you are familiar with C#, I would recommend you to start with writing something like ArrayList in C. Then you can use it in many situations (like this one). 另外,由于您熟悉C#,因此建议您从用C编写类似ArrayList的内容开始。然后,可以在许多情况下使用它(例如这种情况)。

First change your definition to: 首先将您的定义更改为:

typedef struct Scheduler {
    struct ProcessInfo** Processes;
} Scheduler;

Then something like this: 然后是这样的:

Scheduler *s;
s = malloc(sizeof(Scheduler));
if (s == NULL) {
  exit(-1);
}
s = memset(s, 0, sizeof(Scheduler));
/* Or enter your own memory allocation stuff here */
i = 0; /* Or use some other number */
s->Processes[i] = someProcessInfo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM