LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-18-2008, 05:34 PM   #1
PJani
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
[C] Pass pointer of struct to function by refrence


Hy im having a big problems...
Im trying to pass pointer by reference of struct array to function here is my function with struct
Code:
struct TCLIENT_INFO{
		int clientid;
		int sock;
		unsigned char buffer[1024];
		int bconnected;
		long tickconnected;
		long ticklastrec;
		long ticklastsnd;
		unsigned char packet_err;
		unsigned char packet_waits;
		unsigned char token[8];
};

int addcli(struct TCLIENT_INFO** cli_info, unsigned short *ustop){
	int stat = 0;
	
	//increase top bound
	ustop++;
	
	printf("CLI Info Pointer: %p\n", *cli_info);
	if(ustop > 0){
		if(*cli_info==NULL){
			*cli_info = realloc(NULL,(ustop * sizeof(struct TCLIENT_INFO)));
		}else{

			*cli_info = realloc(*cli_info,(ustop * sizeof(struct TCLIENT_INFO)));
		}
	}else if(ustop == 0){
		
	}
	
	return stat;
}

//this main prog is for testing
int main(){
	struct TCLIENT_INFO *cli;
	unsigned short cli_slots = 0;
	struct TCLIENT_INFO *cl2;

	printf("%d\n",addcli(&cli,&cli_slots));
	
	free(cli);
	return 0;
}
i get this error

test.c: In function âaddcliâ:
test.c:64: error: invalid operands to binary *
test.c:66: error: invalid operands to binary *
test.c:69: error: invalid operands to binary *
line 64 is *cli_info = realloc(NULL,(ustop * sizeof(struct TCLIENT_INFO)));
line 66 is *cli_info = realloc(*cli_info,(ustop * sizeof(struct TCLIENT_INFO)));
line 69 is *cli_info = malloc((ustop * sizeof(struct TCLIENT_INFO)));

please help

Last edited by PJani; 11-19-2008 at 12:36 AM.
 
Old 11-18-2008, 05:57 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
ustop is a pointer not an integer

unsigned short *ustop

you need to revisit how you are using ustop
 
Old 11-19-2008, 12:37 AM   #3
PJani
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
ustop should return value by reference from function high bound and it is used to get high bound of this struct array.
 
Old 11-19-2008, 12:49 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
If you want to use it as an int, you have to use *ustop no matter what the variable's purpose is. It makes no sense to multiply a pointer by anything, which is what you're doing in your realloc statements.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
in C, how do I pass a pointer of an array of structures to a function? randomsel Programming 2 02-04-2008 06:23 PM
Parsing struct pointer from main to function. Goblin_C_Noob Programming 15 05-17-2007 12:22 PM
I just want to make sure this is correct, struct pointer array paramter in function. RHLinuxGUY Programming 6 08-20-2006 09:30 PM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration