C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 26th, 2002, 05:00 AM
touchstone_fury touchstone_fury is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: new zealand
Posts: 0 touchstone_fury User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 57 sec
Reputation Power: 0
sending an array too and from the main function...

Hi guys... I have started building a library of small reminder programs for C...

I have managed to successfully send an array from the main funtion to a pass function...

Even managed to send an array from the pass function to the main...

When I try and send one from the main to pass and a new array back to the main then I get one error message, no warnings... saying too few arguments...

PHP Code:
#include <stdio.h>
int pass(int arrayA[]);
main()
{
    
int arrayA[1];
    
arrayA[0] = 1;

    
pass(arrayA);
    
printf("element 0 from function two equals %d \n"pass());
}
pass(int arrayA[])
{
    
int arrayB[1];
       
arrayB[0] = 1;

    
printf("array number 0 from main is %d \n",arrayA[0]);       
   return (
arrayB[0]);




This happens in the printf statement in the main function when I call the pass function...

I have not yet approached pointers... thats next on the list...lol

Any help is appreciated... thanks

Reply With Quote
  #2  
Old September 26th, 2002, 06:13 AM
Beans4You's Avatar
Beans4You Beans4You is offline
Some day I will be a Lambda!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: NJ
Posts: 18 Beans4You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Here is what is happening. You are returning an integer from your pass function just as you should but in your main I think you are trying to treat the pass() as a variable. You need to either declare a variable and then assign it the value which is returned by the pass function or do as I did below and let the pass function return the value directly to the printf.

Code:
#include <stdio.h>
int pass(int arrayA[]);
main()
{
    int arrayA[1];
    arrayA[0] = 1;

    printf("element 0 from function two equals %d \n", pass(arrayA));
}
pass(int arrayA[])
{
    int arrayB[1];
       arrayB[0] = 1;

    printf("array number 0 from main is %d \n",arrayA[0]);       
   return (arrayB[0]);
}


Hope this helps.
Eric

Reply With Quote
  #3  
Old September 26th, 2002, 06:29 AM
touchstone_fury touchstone_fury is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: new zealand
Posts: 0 touchstone_fury User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 57 sec
Reputation Power: 0
thanks a lot...:)

Thanks a lot... I tried it and it works ok funny cause I'm sure that I tried that approach before coming online...lol

I thought the result gave an answer where the printf statement in the main fuction returned a result from the main array...

It works though so cool... will have to try and play some more...

Thanks again... have a good one.

Reply With Quote
  #4  
Old September 26th, 2002, 07:57 AM
rendy's Avatar
rendy rendy is offline
Moderator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: London
Posts: 348 rendy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 17 sec
Reputation Power: 7
Pass your array by reference (&) to avoid unneccesary copy constructor calls..

Reply With Quote
  #5  
Old September 26th, 2002, 12:33 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
>>Pass your array by reference (&) to avoid unneccesary copy constructor calls..
Arrays are already passed by reference in C automatically. For example, pass(arrayA) is a synonym for pass(&arrayA[0]);, so there's no question of a copy constructor being called. To illustrate this further, I made a few modifications to Beans4You's code:
Code:
#include <stdio.h>
int pass(int array[]);
main()
{
  int arrayA[1];
  arrayA[0] = 1;

  printf("Before calling pass() array number 0 from main is %d \n",arrayA[0]);
  pass(arrayA);
  printf("After calling pass() array number 0 from main is %d \n",arrayA[0]);
}
pass(int array[])
{
  array[0] = 2;
  return (1);
}

As you can see, the change to array[0] in pass() will affect arrayA[0] in main().

Reply With Quote
  #6  
Old September 27th, 2002, 05:56 AM
rendy's Avatar
rendy rendy is offline
Moderator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: London
Posts: 348 rendy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 17 sec
Reputation Power: 7
Would that be the same in C++ as well then?
I must admit, I didn't read the bit about a C program, I just assumed it was C++...

In C++ passing parameters by value will result in the complete parameter being copied on to the stack.

This is acceptable for regular types like integer, pointer etc. as these types are generally restricted to four bytes so there is no overhaed.
When passing bigger types, the cost of copying the object on the stack can be prohibitive.

In case of passing classes there will be an additional overhead of invoking the constructor for the temporary copy that is created on the stack. When the function exits the destructor will also be invoked, but thats by the by.

Reply With Quote
  #7  
Old September 27th, 2002, 07:03 AM
Beans4You's Avatar
Beans4You Beans4You is offline
Some day I will be a Lambda!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: NJ
Posts: 18 Beans4You User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I'm sorry, I too did not see that he was talking about C. I have no experience with C, only VC++ 6.0, C++, which was on a DEC Alpha, and GC++.

Would you need to pass LPCTSTR's by value? Sometimes I get a memory access violation during run-time. This can be solved by lowering the size of my LPCTSTR array's.

Eric

Reply With Quote
  #8  
Old September 27th, 2002, 12:25 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,432 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 22 h 29 m 51 sec
Reputation Power: 784
>>Would that be the same in C++ as well then?
Yes it is. Note that this is the case for arrays of any type in both C and C++.

>>Would you need to pass LPCTSTR's by value?
By definition, LP = Long Pointer, so you're passing a pointer, NOT a copy of the array. Anyways, like I said above, when you're passing arrays, C (and C++) automatically pass by reference (i.e. by passing the pointer to the array instead of a copy), so there's no question of decreasing the size of your array or whatever. Hope this clears things up a bit.

Reply With Quote
  #9  
Old September 29th, 2002, 01:08 PM
rendy's Avatar
rendy rendy is offline
Moderator
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: London
Posts: 348 rendy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 17 sec
Reputation Power: 7
Yeah thats cleared it up nicely!
Thanks for the help

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > sending an array too and from the main function...


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway