0

prog.c: In function ‘main’:

prog.c:35:20: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘void *’ [-Wformat=] printf("\n %c \t %d \t identifier\n",c,p); ^

prog.c:47:24: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘void *’ [-Wformat=] printf("\n %c \t %d \t operator\n",ch,p); ^ '''

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
void main()
{
    int i=0,j=0,x=0,n;
    void *add[5],*p;
    char ch,srch,b[15],d[15],c;
    printf("Expression terminated by $:");
    while((c=getchar())!='$')
    {
        b[i]=c;
        i++;
    }
    n=i-1;
    printf("Given Expression:");
    i=0;
    while(i<=n)
    {
        printf("%c",b[i]);
        i++;
    }
    printf("\n Symbol Table\n");
    printf("Symbol \t addr \t type");
    while(j<=n)
    {
        c=b[j];
        if(isalpha(toascii(c)))
        {
            p=malloc(c);
            add[x]=p;
            d[x]=c;
            printf("\n %c \t %d \t identifier\n",c,p);
            x++;
            j++;
        }
        else
        {
            ch=c;
            if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
            {
                p=malloc(ch);
                add[x]=p;
                d[x]=ch;
                printf("\n %c \t %d \t operator\n",ch,p);
                x++;
                j++;
            }
        }
    }
}

'''

taho
  • 1
  • 1
    Why have you added the tag "segmentation-fault"? Does your program crash? If not, please remove that tag. – kiner_shah Feb 22 '22 at 05:47
  • Does this answer the question: https://stackoverflow.com/questions/15292237/printing-a-void-variable-in-c ? – Mike Feb 22 '22 at 05:47
  • 1
    Welcome to SO. Every beginners book should tell you what format specifiers you need to use for what parameter type. What exactly puzzles you about that compiler message? – Gerhardh Feb 22 '22 at 07:19
  • 1
    `c` should be an `int` as `getchar` returns an `int`. – Gerhardh Feb 22 '22 at 07:20
  • 1
    I don't think you understand how `malloc` works: `p=malloc(c);` and `p=malloc(ch);` make no sense. If you want to allocate a single byte, you want `p=malloc(1);`. And you are doing nothing with `p` and `add[]`... what are they for? – SGeorgiades Feb 22 '22 at 07:46
  • 1
    [Why the return type of getchar() is int?](https://stackoverflow.com/q/60332668/995714), [Why must the variable used to hold getchar's return value be declared as int?](https://stackoverflow.com/q/18013167/995714) – phuclv Feb 22 '22 at 08:19

1 Answers1

2

you should use %p in printf just like printf("%p",p);

zhj
  • 478
  • 3
  • 16