C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2012, 04:06 AM
Ricsie Ricsie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Ricsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 8th, 2012, 04:45 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
Pass pointers to arguments (or references in C++). Alternatively, return a structure and decompose the bits of the structure.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Reply With Quote
  #3  
Old November 8th, 2012, 05:46 AM
Ricsie Ricsie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Ricsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #4  
Old November 8th, 2012, 06:09 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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!

Reply With Quote
  #5  
Old November 8th, 2012, 06:46 AM
Ricsie Ricsie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Ricsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old November 8th, 2012, 07:21 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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;
}
Comments on this post
ptr2void agrees!

Reply With Quote
  #7  
Old November 8th, 2012, 10:01 AM
Ricsie Ricsie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Ricsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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,

Reply With Quote
  #8  
Old November 8th, 2012, 10:11 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
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.

Reply With Quote
  #9  
Old November 8th, 2012, 05:22 PM
Ricsie Ricsie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 8 Ricsie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Get >1 variables from a function without using Global variables

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap