473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

passing an array to function

Hi,

I am reading an 256 words of an hard disk drive and storing it in an array 256 volatile shorts and now i want to write the same 256 words starting from 0 to the fourth sector.

i want to know how to pass this array to a function.

Regards,
Ravi
May 19 '08 #1
5 1589
arnaudk
424 256MB
Your question is a bit vague. If you don't know how to pass an array to a function, then here is an example of a function which takes an array of shorts and prints it to the screen:
Expand|Select|Wrap|Line Numbers
  1. void printarray(short * a, int a_size)
  2. {
  3.    for(int i = 0; i < a_size; ++i)
  4.    {
  5.       // printf("arr[%i]=%g\n",i,a[i]); // in C
  6.       std::cout << "arr[" << i << "]=" << a[i] << std::endl; // in C++
  7.    }
  8. }
  9.  
  10. int main()
  11. {
  12.    const int arr_size= 5;
  13.    short arr[arr_size] = {35, -14, 98, 54, 1};
  14.    printarray(arr, arr_size);
  15. }
  16.  
In C++, you should use a reference to the array so the argument list of printarray should be: (short * & a, int& a_size).
Since printarray doesn't change the array, it sould use const keywords to make sure no changes happen accidentally (and this could also lead to some compiler optimizations): (const short * const& a, const int & a_size)
May 19 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Passing arrays to functions is covered in this article: http://bytes.com/forum/thread772412.html.

Since your array is volatile, you may want to pass a copy to your function to avoid race conditions where the array changes while your function is in progress.
May 19 '08 #3
Hi Arnaudak and weaknessforcats,

I know how to pass an array to a function.Still thanks for suggestions.I am using C language for my project.What i am doing:

read_function()
{
reading an array of 255 volatile short variables in a[255]. and passing this array to write_function().
}
write_function()
{
Passing that array so that,my first variable write at the first byte of fourth sector.
}
My qus, how i will retain the values in my array between function calls?

Even i have tried to declare it as static,but i am not getting the desired output.

Regards,
Ravi
May 20 '08 #4
arnaudk
424 256MB
I think I still don't understand your question. If your function calls mess with your array, can't you copy it element by element to another temporary array and restore it later? If you declared it as static, it will retain its value between function calls but it seems that you overwrite this value with read_function.
May 20 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
read_function()
{
reading an array of 255 volatile short variables in a[255]. and passing this array to write_function().
}
write_function()
{
Passing that array so that,my first variable write at the first byte of fourth sector.
}
My qus, how i will retain the values in my array between function calls?
Post your code. This outline shows you are not passing anything to these functions.

Also, a word is usually either 32 or 64 bits while a short is 16. Further, a short is signed so it's really 15. But maybe you are using some operating system that has a 16 bit word. In any case, the array should be unsigned.
May 20 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.