The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Get >1 variables from a function without using Global variables
Discuss Get >1 variables from a function without using Global variables in the C Programming forum on Dev Shed. Get >1 variables from a function without using Global variables C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 8th, 2012, 04:06 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 2 h 11 m 32 sec
Reputation Power: 0
|
|
|
Get >1 variables from a function without using Global variables
Hello,
I always got more variables from function by using Global variables, but what if i cant use global variables? is there any chance t oget 3-4 variables from a function? and if yes by what way?
Also how it works with arrays? hot to get on and more arrays from a function.
Thanks
C Beginner
|

November 8th, 2012, 04:45 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
Pass pointers to arguments (or references in C++). Alternatively, return a structure and decompose the bits of the structure.
|

November 8th, 2012, 05:46 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 2 h 11 m 32 sec
Reputation Power: 0
|
|
|
Yes i know i have t do it somehow with passing the pointers, but it never works for me the good way. so would be great if you can help me with an example to understand it the proper way. so the example... can be:
I have three functions, main(), read(),write()
In read i read something from file to array,
and in write i printf the array,
in main if c = getchar() is r i call function read and if c is w i call function write
Thansk a lot!
|

November 8th, 2012, 06:09 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
This is why you go to school, to learn how to do things. Just asking for help won't teach you how to learn. There are _tons_ of resources available now that weren't when I was learning, take the time to earn your degree!
|

November 8th, 2012, 06:46 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 2 h 11 m 32 sec
Reputation Power: 0
|
|
|
I understand what you mean, and i cant do nothing else just agree, so the i change my question :-) if you can please help me how to look after the information in english, is it passing pointer to function?
because my biggest problem is my english, im hungarian living in slovakia, theres almost nothing about c in slovak language, and our teacher teaches us C in slovak so im unfamiliar with English names, she even pointers calls Ukazovatele or smerniky, so its really confusing.
|

November 8th, 2012, 07:21 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
OK, here is an example:
Code:
#include <stdio.h>
#define ARRLEN 25
void funct(int *intval, char *chrArray, int arrlen){
*intval = 12345;
strncpy(chrArray, "here be tygers", arrlen-1);
}
int main(){
int ival = 0;
char carr[ARRLEN];
strcpy(carr, "I am blank");
printf("Before: ival: %d, carr: %s\n", ival, carr);
funct(&ival, carr, ARRLEN);//note that carr is _already_ a pointer. could also use (in this case, but not in all cases!) sizeof(carr) for length
printf("After: ival: %d, carr: %s\n", ival, carr);
return 0;
}
|

November 8th, 2012, 10:01 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 2 h 11 m 32 sec
Reputation Power: 0
|
|
|
Thanks a lot, this helped me a lot, really thanks
have one question left, can you plese tell me if theres a solution, that we can transfer arrays from void functions? like
void function(void);
there will be array declared
to void function2(void)
there will use for example array[87]
Dont need to tell me how to do it, just if its possible to do it in some not hard way, :-)
Thanks for helping me,
|

November 8th, 2012, 10:11 AM
|
 |
I'm Baaaaaaack!
|
|
Join Date: Jul 2003
Location: Maryland
|
|
|
There are three basic areas of memory for a program. The heap is where you malloc memory from, the stack which 'magically' is created as you enter a routine (and 'magically' cleaned up when you leave) and static memory that was 'allocated' by the compiler when the program was built. Of course, there is memory that can be created as files, shared objects, etc., but those are not what I think you are after. In order to 'share' memory with a subroutine either the memory has to be common to the scope of the two functions ('global'), it must be passed in, so that the calling function 'owns' the memory, or it must be allocated on the heap inside the called function and then it is the callee's responsibility to deallocate the memory. Having said that, by using structures it is possible to 'allocate' memory on the stack through a return value (as I briefly alluded to originally). As a side note, use of the key word 'static' inside a function will result in memory that is no longer on the stack, so retains its value between function calls (note that this type of memory is very problematic when using threads).
So, in your example, if you simply declare a local variable of array inside the function then it exists on the stack and hence goes out of scope when the function returns. While you might be able to access that memory outside the function call, it will be coincidental and is a bug waiting to manifest. You could alternatively malloc some memory and then return a pointer to that memory, but you have to then remember to free it when you are done.
|

November 8th, 2012, 05:22 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 8
Time spent in forums: 2 h 11 m 32 sec
Reputation Power: 0
|
|
|
Thanks a lot for helping me to understand this part of C, i did it somehow similar to your example, and also spent whole day reading about strings and arrays and pointers, and hopefully my knowledge of C is a little bit deeper :-) Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|