Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用指向结构的指针?_C - Fatal编程技术网

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

C 如何使用指向结构的指针?,c,C,我有点问题。我刚从C开始(来自C#背景),我对双指针有问题 我的结构如下: #ifndef __PROCESSINFO_H #define __PROCESSINFO_H struct ProcessInfo { int ProcesId; int Priority; int ExecutionTime; int EllapsedTime; char* ProcessName; }; struct ProcessInfo *ProcessInfo_Alloca

我有点问题。我刚从C开始(来自C#背景),我对双指针有问题

我的结构如下:

#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
实施:

#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);
    }
}

因此,问题是如何在
调度程序\u Allocate
方法中初始化**进程成员?如何向其中添加内容?

指向指针的指针被初始化为指针数组。因此,调用
malloc(count*sizeof(ProcessInfo*))
对其进行初始化。通过这种方式,可以获得指向ProcessInfo的指针数组。然后多次调用
malloc(sizeof(ProcessInfo))
来创建特定的
ProcessInfo
结构,并将指向它们的指针放到数组中

另外,user470379是正确的,您不需要指针指向指针,只需更改数组中的项数即可。但你的想法其实也不错,如果你愿意,你可以坚持下去


另外,由于您熟悉C#,我建议您从用C编写ArrayList之类的东西开始。然后您可以在许多情况下使用它(如本例)。

您不需要双指针来增加/减少大小。只需使用一个普通指针,然后单击鼠标

然后,您可以创建一个
ProcessInfo\u Init
,除了不分配内存外,它可以执行大部分相同的操作,而不是使用
ProcessInfo\u Allocate

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;
}

首先将您的定义更改为:

typedef struct Scheduler {
    struct ProcessInfo** Processes;
} Scheduler;
然后像这样:

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;
另请参见我的示例:

size\u t size=10//或者进程的数量是多少
struct ProcessInfo*process=(struct ProcessInfo*)malloc(size*sizeof(struct ProcessInfo));
如果(!进程)
//也许停止节目?做点什么
进程=&进程;
//后来
int i;
对于(i=0;iProcesId);
等
}

使用
\uu SCHEDULER\u H
是无效的C。请将其重命名为
SCHEDULER\u H
或任何其他不以下划线开头的名称。R:我认为您在这里太迂腐了。检查
malloc
的返回值从来都不是一个坏主意。谢谢,我忘了:P我一直在使用包装器函数,自动为我完成所有这些。编辑以反映这一点。伟大的答案!这正是我想要的。实际上,这会创建一个列表/堆栈类型的对象,您只需以非常简单的方式添加或删除项即可。我将在此基础上实现Add()、AddAt()、Remove()、RemoveAt()。。。太酷了!我想知道我是否可以用void**代替强类型对象来做同样的事情?@bleepzter:是的,你可以用“void”代替“struct ProcessInfo”。但是,每次编译器需要类型信息时,都需要强制转换指针。使用带有宏/模板的预处理器,您可能可以更优雅地解决这个问题,但我对c的了解还不足以告诉您任何细节。
typedef struct Scheduler {
    struct ProcessInfo** Processes;
} Scheduler;
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;
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++;
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
}