Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

#include errors I can't solve

+7 votes
asked Jul 5, 2023 by mega aids (200 points)
edited Jul 6, 2023 by mega aids

This is my code:

#include <stdio.h>
#include "addStruct.h"

/*This is the addStruct class:

struct addStruct
{
    char name[50];
    char address[50];
    int zipcode;
}

*/
#include <stdlib.h>
void main()
{
    struct addStruct * address[3];
    int j = 0;
    while(1 != 0)
    {
        address[j] = (struct addStruct*)malloc(sizeof(struct addStruct));
        printf("What name belongs to the address? ");
        fgets(address[j]->name, 50, stdin);
        printf("What address are you adding? ");
        fgets(address[j]->address, 50, stdin);
        printf("What zipcode is the address in? ");
        scanf(" %d", &address[j]->zipcode);
        j++;
        getchar();
        printf("Do you want to continue adding addresses? (Y/N) ");
        char input;
        input = getchar();
        if(input == 'n' || input == 'N')
        {
            break;
        }
        getchar();
    }
    for(int i = 0; i < j; i++)
    {
        getchar();
        printf("Name %d: %s", i+1, address[i]->name);
        printf("Address %d: %s", i+1, address[i]->address);
        printf("Zipcode %d: %d", i+1, address[i]->zipcode);
    }
    return;
}

These are my errors: 

/usr/lib/gcc/x86_64-linux-gnu/11/include/stddef.h:321:9: error: expected ‘;’, identifier or ‘(’ before ‘int’
  321 | typedef __WCHAR_TYPE__ wchar_t;
      |         ^~~~~~~~~~~~~~
In file included from main.c:11:
/usr/include/stdlib.h:933:20: error: expected declaration specifiers or ‘...’ before ‘wchar_t’
  933 | extern int mbtowc (wchar_t *__restrict __pwc,
      |                    ^~~~~~~
/usr/include/stdlib.h:937:31: error: expected declaration specifiers or ‘...’ before ‘wchar_t’
  937 | extern int wctomb (char *__s, wchar_t __wchar) __THROW;
      |                               ^~~~~~~
/usr/include/stdlib.h:941:25: error: expected declaration specifiers or ‘...’ before ‘wchar_t’
  941 | extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
      |                         ^~~~~~~
/usr/include/stdlib.h:946:39: error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
  946 |                         const wchar_t *__restrict __pwcs, size_t __n)
      |                                       ^
I have no idea why it doesn't work, any explanation would be superb. Thanks in advance

3 Answers

+1 vote
answered Jul 6, 2023 by Peter Minarik (86,240 points)

You have further files you include (such as addStruct.h) so I cannot test what's going on as the compilation fails due to the file missing.

Try including <wchar.h> and see if that solves your problem. This is just a guess.

Or perhaps you're using a Windows header or Windows type that's not available on a Linux based system, such as OnlineGDB.

If you can share your whole project, one could try to reproduce your issue.

commented Jul 6, 2023 by mega aids (200 points)
Oops. Added the addStruct definition where I included it
commented Jul 7, 2023 by Peter Minarik (86,240 points)
If I copy your code to a main.c and create a new file called addStruct.h and put your structure definition in there (plus a semicolon after it!!!), but nothing else, then the code compiles as a valid C program and runs.

In other words, your code is different from what I have, as I cannot reproduce the issue.

You can share your whole project with the orange SHARE button (2 buttons right from the RUN button). Copy the link from there so your actual project is shared and no detail will be missed.
0 votes
answered Aug 25, 2023 by Gulshan Negi (1,580 points)

There are some logical error with your code.

Try below code and confirm is it working or not.

#include <stdio.h>
#include "addStruct.h"
#include <stdlib.h>

int main() {
    addStruct *address[3];
    int j = 0;
    while (1 != 0) {
        address[j] = (addStruct*)malloc(sizeof(addStruct)); // No 'struct' keyword
        printf("What name belongs to the address? ");
        fgets(address[j]->name, 50, stdin);
        printf("What address are you adding? ");
        fgets(address[j]->address, 50, stdin);
        printf("What zipcode is the address in? ");
        scanf(" %d", &address[j]->zipcode);
        j++;
        getchar();
        printf("Do you want to continue adding addresses? (Y/N) ");
        char input;
        input = getchar();
        if (input == 'n' || input == 'N') {
            break;
        }
        getchar();
    }
    for (int i = 0; i < j; i++) {
        getchar();
        printf("Name %d: %s", i + 1, address[i]->name);
        printf("Address %d: %s", i + 1, address[i]->address);
        printf("Zipcode %d: %d", i + 1, address[i]->zipcode);
    }
    
    // Free allocated memory
    for (int i = 0; i < j; i++) {
        free(address[i]);
    }

    return 0; // 'int' return type for main
}

Thanks

0 votes
answered Aug 25, 2023 by yash bhatt (220 points)

The errors are related to the use of the wchar_t type, which is a wide character type, and these errors are occurring because you're including headers that define wchar_t without including the necessary headers.

To fix these errors, you can include the necessary headers at the beginning of your code:
 

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h> // Include this header for wchar_t-related functions

struct addStruct
{
    char name[50];
    char address[50];
    int zipcode;
};

int main() // Change void main() to int main()
{
    struct addStruct *address[3];
    int j = 0;
    while (1 != 0)
    {
        address[j] = (struct addStruct *)malloc(sizeof(struct addStruct));
        printf("What name belongs to the address? ");
        fgets(address[j]->name, 50, stdin);
        printf("What address are you adding? ");
        fgets(address[j]->address, 50, stdin);
        printf("What zipcode is the address in? ");
        scanf(" %d", &address[j]->zipcode);
        j++;
        getchar();
        printf("Do you want to continue adding addresses? (Y/N) ");
        char input;
        input = getchar();
        if (input == 'n' || input == 'N')
        {
            break;
        }
        getchar();
    }
    for (int i = 0; i < j; i++)
    {
        getchar();
        printf("Name %d: %s", i + 1, address[i]->name);
        printf("Address %d: %s", i + 1, address[i]->address);
        printf("Zipcode %d: %d", i + 1, address[i]->zipcode);
    }

    // Free the allocated memory
    for (int i = 0; i < j; i++)
    {
        free(address[i]);
    }

    return 0; // Change return; to return 0;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...