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 7th, 2012, 04:30 AM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old November 7th, 2012, 05:00 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
There should be no problem with that.

Overrunning local arrays is usually more immediately catastrophic though, as that usually trashes the call stack.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old November 7th, 2012, 06:56 AM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old November 7th, 2012, 07:18 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 9 m 51 sec
Reputation Power: 1774
> 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.

Reply With Quote
  #5  
Old November 7th, 2012, 09:29 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,127 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 18 h 23 m 31 sec
Reputation Power: 1949
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".

Reply With Quote
  #6  
Old November 7th, 2012, 02:35 PM
serafon serafon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 13 serafon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old November 8th, 2012, 03:24 AM
miteshaegis miteshaegis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 miteshaegis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m
Reputation Power: 0
Hi,

I think its possible, you can declare array in function. Its properly work.

Thnaks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Passing local array to a function

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