首页 > 解决方案 > C 头文件和#define 指令

问题描述

头文件名为 myh.h。在这个程序中,如何在这个数组中存储更多的#define 值作为 p[]?存储了两个值,如何在给出注释的主函数中访问这些值?程序中存在编译错误。

#include <stdio.h>
#include <string.h>
#define p [] { "parthinb ", "baraiyab " }
#define u "parthin"
int account(char name[10])
{
    printf("Welcome %s", name);
    return 0;
}
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "myh.h"

int main()
{
    char un[20], pass[10], c;
    int i;
start:
    printf("Enter USER NAME::");
    gets(un);
    printf("Enter Password of 8 Digitis::");
    for (i = 0; i < 8; i++)
    {
        c = getch();
        pass[i] = c;
        c = '*';
        printf("%c", c);
    }
    pass[i] = ' ';
    if (strcmp(un, u) == 0)
    {
        if (strcmp(pass, p[]) == 0)
        {
            printf("\nCORRECT\n");
            account(un);
        }
        else
        {
            printf("\nPassword mis-match\n");
            goto start;
        }
    }
  else
{
    printf("\nUSER Name or password does not match.\n");
    goto start;
}

}

标签: cc-preprocessorheader-files

解决方案


您不能使用 #define 来存储 char* 数组。检查此链接。如果您以后不需要更改它,您可以将数组定义为 const:

const char* p[] = {"parthinb ", "baraiyab "};

您可以使用其索引访问 p 的每个元素:

p[index_of_that_element]

推荐阅读