help me about error" expected primary-expression before "struct" "

When I write the following function gives the following error What is the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <cstdlib>
#include <iostream>

using namespace std;
void inserttchr(char,char,char,int,int);
struct tchr
{
char *tname,*tfield,*tm;
int tcode;
};
int main(int argc, char *argv[])
{
struct tchr t[100];
char *tn,*tf,*tm;
int i,tcode;
for(i=0;i<100;i++)
{
gets(*tn)
gets(*tf)
gets(*tm)
scanf("%d",&tcode)
inserttchr(*tn,*tf,*tm,tcode,i);
}
        
        
    system("PAUSE");
    return EXIT_SUCCESS;
}
void inserttchr(char *n,char *f,char *m,int code,int index)
{
struct tchr t[index].*tname=*n;
struct tchr t[index].*tfield=*f;
struct tchr t[index].*tm=*m;
struct tchr t[index].tcode=code;
getch();
}

and errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"lib/gcc/mingw32/3.4.2/include"  -I"include/c++/3.4.2/backward"  -I"include/c++/3.4.2/mingw32"  -I"include/c++/3.4.2"  -I"include"   

main.cpp: In function `void inserttchr(char*, char*, char*, int, int)':
main.cpp:31: error: expected primary-expression before "struct"
main.cpp:31: error: expected `;' before "struct"
main.cpp:32: error: expected primary-expression before "struct"
main.cpp:32: error: expected `;' before "struct"
main.cpp:33: error: expected primary-expression before "struct"

main.cpp:33: error: expected `;' before "struct"
main.cpp:34: error: expected primary-expression before "struct"
main.cpp:34: error: expected `;' before "struct"
main.cpp:35: error: `getch' undeclared (first use this function)
main.cpp:35: error: (Each undeclared identifier is reported only once for each function it appears in.)

make.exe: *** [main.o] Error 1

Execution terminated

1) line 13. You do not need to write struct tchr . Just tchr will be fine.

2) Using uninitalizated pointers tn tm and tf (illegal and can lead to crash)

3) Lines 31-34 I am at loss waht happens here.
Looks like you are creating 4 variables of the same name (inllegal) which are variable length arrays (illegal) and using member selection in time of initialization (illegal)

If you want to modify variable declared in main(), you should pass it to your function.

Also there is no sense to pass characters by pointer. Use palin old chars here.
This happens in lines 31-34 tm and tf, tد, Tcode that were taken in each part of the Main() Plan struct stores.
Unfortunately I am not native English speakers. I do not know if you've noticed my posts?
1
2
3
4
5
6
7
void inserttchr(class="quote">
class="qd">tchr* t
, char *n,char *f,char *m,int code,int index) { t[index].tname = n; t[index].tfield = f; t[index].tm = m; t[index].tcode = code; }


Fixed function. However you have so many problems in other parts that it is probably going to crash right away.
Last edited on
Thank you
there are a couple of issues that spring to mind.

Firstly, t[] is declared locally to main() but you are trying to access it from insertchr()

you are using alot of character pointers, these usually cause problems for beginners, replace them with character arrays to simplify your code.

you can improve this code greatly by using std::string


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdlib>
#include <iostream>

using namespace std;

struct tchr
{
    char tname[100], tfield[100], tm[100];
    int tcode;
};

void inserttchr(char*,char*, char*, int, struct tchr*);

int main(int argc, char *argv[])
{
    struct tchr t[100];
    char tn[100], tf[100] ,tm[100];
    int i,tcode;

    for(i=0;i<100;i++)
    {
        gets(tn)
        gets(tf)
        gets(tm)
        scanf("%d",&tcode)
        inserttchr(tn ,tf, tm, tcode, &t[i]);
    }
           
    system("PAUSE");
    return EXIT_SUCCESS;
}

void inserttchr(char *n,char *f, char *m, int code, struct tchr* pEntry)
{
    strcpy(pEntry->tname,n);
    strcpy(pEntry->tfield,f);
    strcpy(pEntry->tm,m);
    pEntry->tcode = code;
    getch();
}
Topic archived. No new replies allowed.