UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » How to create a funcion that receive and manipule struct!?

#1 2004-03-17 11:27 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#2 2004-03-18 02:05 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#3 2004-03-18 02:56 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#4 2004-03-18 09:14 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

ideas ?

ps.: i couldn't put it small code to work  :roll:

Offline

#5 2004-03-19 12:01 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: How to create a funcion that receive and manipule struct!?

typedef struct stx_struct {
    char *lan;
    int line;
    int count;
} STX;

Offline

#6 2004-03-19 01:26 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: How to create a funcion that receive and manipule struct!?

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;

Offline

#7 2004-03-19 03:34 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#8 2004-03-19 10:24 PM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#9 2004-03-31 09:28 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

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.

Offline

#10 2004-04-01 07:46 AM

Nope
Administrator
From: Germany
Registered: 2004-01-24
Posts: 385
Website

Re: How to create a funcion that receive and manipule struct!?

Offline

#11 2004-04-05 08:07 PM

felix
Member
Registered: 2003-12-04
Posts: 171

Re: How to create a funcion that receive and manipule struct!?

Hi nope!

Thkz for the link, i will take a look. :)

Regards

Offline

  • Index
  • » C
  • » How to create a funcion that receive and manipule struct!?

Board footer

Powered by FluxBB