View Full Version : How to create a funcion that receive and manipule struct!?
felix
03-17-2004, 10:27 PM
Hi,
I created a struct (in a public part) of my code, now i want in main() funciona call another function that must recive the struct contents and change it. Example:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
typedef struct{
char *lan;
int line;
int count;
} stx;
void line2size(char *ReceiveStruct, FILE *f){
// Here i will put my routines that will change value from line and count.
}
int main(){
// Some routines...
line2size(struct stx, filePointer);
printf("\n\n%d %d",stx->list,stx->count);
return(0);
}
How to do it ?? Someone can give a example ? :)
Thkz a lot.
Regards.
struct STX{
char *lan;
int line;
int count;
};
STX stx
void line2size(STX *ReceiveStruct, FILE *f){
// example:
ReceiveStruct->count=0;
// the same:
stx.count=0;
}
int main(){
// Some routines...
line2size(&stx, filePointer);
printf("\n\n%d %d",stx.list,stx.count);
return(0);
}
This passes the struct stx of type STX via a pointer to the subroutine. As it
is a pointer you have to use the "->" to reference the content. In main it is
used directly, therefore it is the ".". Just to mention it, you have declared
stx globally, so actually there is no need to pass the pointer to that
subroutine of yours, you could use "stx.count=0;" also.... it is global after
all.
felix
03-18-2004, 01:56 PM
Hi!! :)
Thkz a lot nope! ;-)
But it doesn't work at my linux box.. :cry:
$ cat l.c
#include <stdio.h>
#include <string.h>
struct STX{
char *lan;
int line;
int count;
};
STX stx;
void line2size(STX *ReceiveStruct, FILE *f){
// example:
ReceiveStruct->count=0;
// the same:
stx.count=0;
}
int main(){
FILE filePointer;
// Some routines...
line2size(&stx, filePointer);
printf("\n\n%d %d",stx.list,stx.count);
return(0);
}
$ gcc -o x l.c
l.c:12: parse error before `stx'
l.c:12: warning: data definition has no type or storage class
l.c:14: parse error before `*'
l.c: In function `line2size':
l.c:17: `ReceiveStruct' undeclared (first use in this function)
l.c:17: (Each undeclared identifier is reported only once
l.c:17: for each function it appears in.)
l.c:19: request for member `count' in something not a structure or union
l.c: In function `main':
l.c:32: request for member `list' in something not a structure or union
l.c:32: request for member `count' in something not a structure or union
Some ideas ? :)
thkz a lot.
Regards.
felix
03-18-2004, 08:14 PM
ideas ?
ps.: i couldn't put it small code to work :roll:
RobSeace
03-18-2004, 11:01 PM
[People, remember your code tags/button, when posting code,
please!]
The problem is the struct definition does NOT automatically
define a typedef name in plain old C, as Nope was apparently
assuming it does... (Or, simply made a typo, and left out the
intended typedef...) I believe C++ does such auto-typedef'ing of
structs, but I'm not sure... In any case, it's evil and perverse, and
you shouldn't do it... ;-)
Simply change the struct definition like so:
typedef struct stx_struct {
char *lan;
int line;
int count;
} STX;
Then, the rest of Nope's code should work ok...
Thanks Rob. You are right, C++ does make that automatically. On the
other hand I didn't want to typedef it, I usually just add the struct at all
other places:
struct STX stx;
void line2size(struct STX *ReceiveStruct, FILE *f){
A FILE has always to be a pointer, so in your main() you have to write
FILE *filepointer;
felix
03-19-2004, 02:34 PM
Hello!! :)
First Thkz for help!
Second, sorry for forget the tag code.. :cry:
The code worked! If i want use two or more structs, how could i do it in the correct way ?
I done in this way:
#include <stdio.h>
#include <string.h>
typedef struct stx_Struct{
char *lan;
int line;
int count;
} STX;
STX stx;
typedef struct Second_Struct{
char *lan;
int line;
int count;
} SC;
SC sc;
void line2size(STX *ReceiveStruct, FILE *f){
// example:
ReceiveStruct->line=4;
/* With this work-arround can't use more it...
stx.count=0; */
// the same:
ReceiveStruct->count=3;
}
int main(){
FILE *filePointer;
// Some routines...
line2size(&sc, filePointer);
printf("\n\n%d %d",sc.line,sc.count);
line2size(&stx, filePointer);
printf("\n\n%d %d",stx.line,stx.count);
return(0);
}
It, "work", but i belive that isn't correct make it, since i'm using always the some type struct for all structs, it yet show me a "warning":
# gcc -o x l.c
l.c: In function `main':
l.c:41: warning: passing arg 1 of `line2size' from incompatible pointer type
# ./x
4 3
4 3
It works, but how to do it in a elegant way ? :)
thkz
Regards.
A defined struct is kinda variable type. So if you want two structs with the
same "structure" you define 2 variables of the same type:
typedef struct stx_Struct{
char *lan;
int line;
int count;
} STX;
STX stx;
STX sc;
Then the warning vanishes. The warning said that it were two different
structeres you used. If you'd done that with two really different structures the result could have been unpredictable, besides it wouldn't work then.
Perhaps you should hit your book again? Those things are very basic.
felix
03-31-2004, 08:28 PM
Hi nope!
Thkz a lot, it worked :)
ps.: I will get a C book and read, i never had read any book of C :)
Regards.
You can also take a look here:
http://www.ultimategameprogramming.com/index2.php
They have some nice tutorials for the basic stuff. You can also find
some resources in the web that,while perhaps not as good as a book
would at least cost nothing (as long as you have a flatrate of course).
felix
04-05-2004, 08:07 PM
Hi nope!
Thkz for the link, i will take a look. :)
Regards
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.