The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Passing local array to a function
Discuss Passing local array to a function in the C Programming forum on Dev Shed. Passing local array to a function 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 7th, 2012, 04:30 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
|
Passing local array to a function
If I declare an array locally and then pass it to a function, can i use the values of the array in that function? Or is not guaranteed? If so should i declare the array as global or static? Thanks.
|

November 7th, 2012, 05:00 AM
|
 |
Contributed User
|
|
|
|
|
There should be no problem with that.
Overrunning local arrays is usually more immediately catastrophic though, as that usually trashes the call stack.
|

November 7th, 2012, 06:56 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem There should be no problem with that.
Overrunning local arrays is usually more immediately catastrophic though, as that usually trashes the call stack. |
Why is it not a problem? It's going out of scope. Can't any other function etc write over it? Or you say, it is on the stack and it is not overwritten until the stack is full?
|

November 7th, 2012, 07:18 AM
|
 |
Contributed User
|
|
|
|
> If I declare an array locally and then pass it to a function
Be careful with your English semantics.
This is pass an array TO a function.
Code:
void printMsg ( void ) {
char msg[]="hello world");
printf("%s\n",msg);
}
There is no scope issue, since the array remains a valid object whilst printf is doing it's thing.
When printMsg() itself returns, then msg goes out of scope.
This is returning an array FROM a function, which is broken.
Code:
char *getMsg ( void ) {
char msg[]="hello world");
return msg;
}
msg goes out of scope with the function return, so anything doing p = getMsg(); is going to find p point nowhere useful.
Edit: clarify
Last edited by salem : November 7th, 2012 at 10:00 AM.
|

November 7th, 2012, 09:29 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Seraphon, in salem's example:
Code:
char *getMsg ( void ) {
char msg[]="hello world");
return msg;
}
the msg array, being an auto class local array, is created on the stack and goes out of existence when you return from the function, so the pointer that's returned is pointing to memory that's about to get reused by other functions. Won't work.
However, if you declare that local array as static:
Code:
char *getMsg ( void ) {
static char msg[]="hello world");
return msg;
}
then it is created in static memory along with the global variables and persists (ie, continues to exist) after the function call. Of course, its name is only known inside the function, but since you have a pointer to it that provides you with access to it.
And as salem advises, please review the difference between the English language concepts of "to" and "from".
|

November 7th, 2012, 02:35 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by dwise1_aol
And as salem advises, please review the difference between the English language concepts of "to" and "from". |
actually there is no problem with my telling, i confused the scope. Because i thought that, if I call a function from a function I'll be out of scope until I return from the function that I'd called. Sorry and thanks for the clarification.
|

November 8th, 2012, 03:24 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 35 m
Reputation Power: 0
|
|
|
Hi,
I think its possible, you can declare array in function. Its properly work.
Thnaks!
|
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
|
|
|
|
|