parthin bariya
parthin bariya

Reputation: 33

C header files, and #define directive

Header file is name myh.h. In this program how to store more #define value as p[] in this array? Two values are stored and how to access these values in main function where the comment is given? There is compilation error in the program.

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

}

Upvotes: 1

Views: 218

Answers (2)

no746
no746

Reputation: 558

You cannot use #define for storing an array of char*. Check this link. You can define your array as const if you don't need to change it later:

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

and you can access each element of p with its index:

p[index_of_that_element]

Upvotes: 1

dash-o
dash-o

Reputation: 14442

On surface, you can just define store the list of p values in a variables, and avoid the '#define'

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

You can refer to p[i] in strcmp, etc.

Upvotes: 0

Related Questions