Struct and pointers. Pointer passed into a function looses the adress it points to

Greetings. sem is a pointer to semantic which is a struct type variabl.
I pass the sem into function yylex so i can fill the semantic.i and semantic.s(s points to an array).
The problem is that when sem->i = a; is used inside yylex function, sem->s stops showing to the array. Can anyone solve this? thank you.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
using namespace std;

union SEMANTIC_INFO
{
	int i;
	char *s;
}semantic;


int yylex(FILE *fpointer, SEMANTIC_INFO *sem)
{
	char c;
	int i=0;
	int j=0;
	int a=0;
	c = fgetc (fpointer);
	while(c != EOF)
	{
		if(isalpha(c))
		{
		   do
		   {
		       sem->s[i] = c; //<---there is the error when i run it
               c = fgetc(fpointer);
			   i++;
		   }while(isalnum(c));
		return 1;
		}
		else if(isdigit(c))
		{
			char arithmoi[20];
			do
			{
				arithmoi[j] = c;
				j++;
				c = fgetc(fpointer);
			}while(isdigit(c));
		    a = atoi(arithmoi);
                    sem->i = a; //->>when i use this and return the next time yylex will be called and
// the next word starts with a letter the error occurs
			return 2;
		}
		else
		{
			do
			{
				c=fgetc(fpointer);
			}while(isalnum(c) == false && c != EOF);
		}
	}
	cout << "end of file" << endl;
	return 0;
}

int main()
{
	char str[20];
	int i,k;
	char c[20];
	int counter1 = 0;
	int counter2 = 0;
	for(i=0; i < 20; i++)
	{
		c[i] = ' ';
	}
    SEMANTIC_INFO *sema = &semantic;
	semantic.s = c;
	cout << "dwste to onoma tou arxeio (mazi me to .txt)" << endl;
	scanf ("%s", str);
    FILE *pFile;
	pFile = fopen (str , "r");
	do
	{
	   k = yylex( pFile, sema);
	   if(k == 1)
	   {
		   counter1++;
		   cout << "it's type is alfanumeric and it's: ";
	      for(i=0; i<20; i++)
	      {
              cout << semantic.s[i] ;
          }
	      cout <<endl;
	      for(i=0; i < 20; i++)
	      {
		      c[i] = ' ';
	      }
	   }
	   else if(k==2)
	   {
		   counter2++;
		   cout << "it's type is digit and it's: "<< semantic.i << endl;
	   }
	}while(k != 0);
	cout<<"the alfanumeric are : " << counter1 << endl;
    cout<<"the digits are: " << counter2 << endl;
	fclose (pFile);
	system("pause");
    return 0;
}
Last edited on
You need to read up on unions.

But to solve your immediate problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
    SEMANTIC_INFO *sema = &semantic;
	//semantic.s = c; //Move this from here
	cout << "dwste to onoma tou arxeio (mazi me to .txt)" << endl;
	scanf ("%s", str);
    FILE *pFile;
	pFile = fopen (str , "r");
	do
	{
	   semantic.s = c; //and put it here
	   k = yylex( pFile, sema);
	   if(k == 1)
	   {
		   counter1++;




But you need to read up on unions.
A union does not have seperate members like a struct.
In a union all the members share the same space.
Once you understand that - you will find out whythis bit of
code below taken from the from the yylex function was causing a crash (and why the solution I suggested above would avoid the crash )


1
2
3
4
5
6
7
8
		
j++;				
c = fgetc(fpointer);
	}while(isdigit(c));
a = atoi(arithmoi);
     sem->i = a; //->>when i use this and return the next time yylex will be called and
// the next word starts with a letter the error occurs
return 2;


In reality most people would have used a struct rather than a union for this;
Last edited on
thank you very much this was really helpfull..i found too the first answear but i couldn't make it perfect :/
Topic archived. No new replies allowed.