C program - Structure marked as undeclared in compiler

亡梦爱人 提交于 2020-01-05 04:16:09

问题


I am trying to write a program to collect security information about a file and convert it to human readable information. However, I am facing a problem with initializing a pointer to structure:

#include <stdio.h>
#include <aclapi.h>
#pragma comment(lib, "advapi32.lib")
struct file_perms {
    char user_domain[2050];
    unsigned long user_mask;
};
static myfunc (){

PSECURITY_DESCRIPTOR pSD = NULL;
PACL pDACL = NULL;
char *file = "D:/code/test.c";
ACL_SIZE_INFORMATION aclSize;

ULONG result = GetNamedSecurityInfo(file,SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, &pSD);
if (ERROR_SUCCESS != result) {
    printf( "GetNamedSecurityInfo Error %u\n", result );
}
if(pDACL != NULL){printf ("2\n");}
//ACL_SIZE_INFORMATION aclSize = {0};
ZeroMemory(&aclSize, sizeof(ACL_SIZE_INFORMATION));
    if(pDACL != NULL){   
        if(!GetAclInformation(pDACL, &aclSize, sizeof(aclSize),
            AclSizeInformation)){
            printf("GetAclInformation Error \n");
            return 0;
        }
        printf("AceCount %d\n",aclSize.AceCount);
    }
file_perms *fp = new file_perms[aclSize.AceCount];

}

While compiling, I am getting the following error. getnamed.c

getnamed.c(34) : error C2065: 'file_perms' : undeclared identifier
getnamed.c(34) : error C2065: 'fp' : undeclared identifier
getnamed.c(34) : error C2065: 'new' : undeclared identifier
getnamed.c(34) : error C2106: '=' : left operand must be l-value
getnamed.c(34) : error C2146: syntax error : missing ';' before identifier 'file
_perms'
getnamed.c(34) : error C2065: 'file_perms' : undeclared identifier
getnamed.c(34) : error C2109: subscript requires array or pointer type

Can someone help me understand why is file_perms marked as undeclared identifier? While it is declared as a structure already?

Thank you for your help.


回答1:


You should have

struct file_perms *fp = new file_perms[aclSize.AceCount];

or create type at begining:

typedef struct file_perms {
    char user_domain[2050];
    unsigned long user_mask;
}file_perm;

and later you can use it like

file_perms *fp;
fp = (file_perms*)malloc(aclSize.AceCount * sizeof(file_perms));

BTW : operator new is c++ syntax, not pure C, you are most probably trying to compile C++ code as C




回答2:


Because you are compiling your code as C code. And it's C++.

If you wish to compile it as C, try this:

typedef struct file_perms_ {
    char user_domain[2050];
    unsigned long user_mask;
} file_perms;



回答3:


Change

struct file_perms{ 
    char user_domain[2050]; 
    unsigned long user_mask; 
}; 

to this will solve your problem:

struct{ 
    char user_domain[2050]; 
    unsigned long user_mask; 
}file_perms; 


来源:https://stackoverflow.com/questions/10764969/c-program-structure-marked-as-undeclared-in-compiler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!