LinuxQuestions.org
Help answer threads with 0 replies.
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 10-27-2007, 05:27 AM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
void variables in c functions


Hi, i'm wanting to be able to pass any type of variable to a function (penultimately it's a wrapper for fwrite). I'm trying the void type, but am having trouble finding reference to this usage.

I keep changing and trying different ways, but here's an example:
Code:
void test(const void *i)
{
	int j = (int)i;
	printf("i contents: %d \n", j);
}
I'm about to try some variations with memcpy etc, but any help would be greatly appreciated!
 
Old 10-27-2007, 07:45 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Maybe this will help -- a call to fwrite() would look like this:
Code:
(void) fwrite ((void *) buf, sizeof (char), n, stdout);
Similarly, a call to memcpy() would look something like this:
Code:
(void) memcpy ((void *) buf2, (void *) buf1, sizeof (buf1));
I think what you're trying to do is const (void *) i?
 
Old 10-27-2007, 08:24 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
In your test function, you take a pointer, but then you cast the value of the pointer (i.e. the memory address of the pointer) to an int and print that. You need to de-reference your pointer before you print it:
Code:
void test(const void *i)
{
        int *j = (int*)i;
        printf("i contents: %d \n", *j);
}

/* OR try it all in one go:
void test(const void *i)
{
        printf("i contents: %d \n", *((int*)i));
}
*/
 
Old 10-27-2007, 08:57 AM   #4
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
Cheers...
How are you calling it because I keep getting errors, e.g.
Quote:
error: void value not ignored as it ought to be
 
Old 10-27-2007, 10:00 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Code:
int somevalue = 3142;
test(&somevalue);
 
Old 10-27-2007, 02:07 PM   #6
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
I'm still getting same error...
I'll now look into compiler because i've had some other weird results today! (gcc version 4.1.2 20070502 (Red Hat 4.1.2-12))
 
Old 10-27-2007, 02:29 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Paste your code, preferably a minimal version which is just enough to duplicate your error. Also paste the command you use to compile/link the program.
 
Old 10-27-2007, 02:43 PM   #8
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
I'm only testing at moment, so it's brief...(and basically my code was same as your first func):

Code:
#include<stdio.h>

void test1(const void *i)
{
	int *j = (int*)i;
	printf("i contents: %d \n", *j);
}

void test2(const void *i)
{
	printf("i contents: %d \n", *((int*)i));
}

int main()
{
	int k = 6284;
	int *j = &k;
	int iret;
	
	iret = test1(&k);
	iret = test2(j);
	
	return 0;
}
I'm compiling with gcc:
Code:
gcc -ansi -c main.c -o main
...and here's the error from this setup:
Quote:
main.c: In function ‘main’:
main.c:46: error: void value not ignored as it ought to be
main.c:47: error: void value not ignored as it ought to be
 
Old 10-27-2007, 02:47 PM   #9
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
Sorry, revise the compile line:
Quote:
gcc -ansi main.c -o main
 
Old 10-27-2007, 02:55 PM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Aha I see. The functions test1 and test2 are defined as returning nothing - void. In the context of a return value to a function this means "no value is returned". Yet you try to assign the return value to the variable iret, which is an int. This is the source of the error.

Either you need to change the return type of the functions to int (and return something), or get rid of these assignments.
 
Old 10-27-2007, 03:03 PM   #11
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33
Yep, I likes that, originally and ultimately there is a return so... Thanks very much for that I felt like I was going mad!
 
Old 10-27-2007, 03:09 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
C does that to a man.
 
  


Reply

Tags
cast, void



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
LXer: OpenOffice.org Calc functions, part 1: Understanding functions LXer Syndicated Linux News 0 03-31-2007 12:01 PM
Send variables between functions using pipes. rastadevil Programming 1 03-21-2006 10:26 PM
Do Perl functions usually modify variables, or return modified copies? Why? johnMG Programming 3 02-06-2005 10:22 PM
C: setting functions equal to variables LuderForChrist Programming 9 07-14-2004 02:37 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM

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

All times are GMT -5. The time now is 08:17 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