473,398 Members | 2,427 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,398 software developers and data experts.

copy array from one function to another

Dear all, I wish to copy the aray aRSG from the first function to the next. Please help, Urgent. I have attached my code
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. // RANDON SEQUENCE GENERATOR FUNCTION
  7. char RSG(int x)
  8. {
  9.      char RandomRSG[5] = {'a','b','c','d'}; //array for sequence to be generated
  10.      int iRSG;     // variable for counting for loop
  11.     char vRSG; // character variable
  12.     char aRSG[50];    //array to store random sequence
  13.     int countRSG = 0;// counter
  14.     int i;
  15.     srand(1);
  16.     printf("RANDOM SEQUENCE GENERATOR WILL NOW GENERATE A,B,C OR D RANDOMLY\n\n");
  17.  
  18.     for (iRSG=0; iRSG<20; iRSG++)
  19.         {
  20.         if((rand()%7) == 0){
  21.               vRSG = RandomRSG[3];
  22.               }
  23.               else if((rand()%9) == 0){
  24.               vRSG = RandomRSG[2];
  25.               }
  26.               else if((rand()%3) == 0){
  27.               vRSG = RandomRSG[1];
  28.               }
  29.               else if(rand()%rand()!=0){
  30.               vRSG = RandomRSG[0];
  31.             }
  32.             aRSG[countRSG] = vRSG;
  33.             countRSG++;
  34.  
  35.  
  36.  
  37.  
  38.     }
  39.     for (i=0; i < 20; i++)
  40.         printf("%c\n", aRSG[i]);
  41.     return aRSG[x];
  42. }
  43.  
  44. InputSelector() {
  45.     int y =0;
  46.     RSG(y);
  47.     int i=0, z=0, a=0, b=0, c=0, d=0, x=0;
  48.     char Mac1Array [20], Mac2Array [20], Mac3Array [20], Mac4Array [20];
  49.     char j,q=0;
  50.     char Selector[50] = aRSG[50];
  51.  
  52.  
  53.     for(i = 0; i<20; i= i+1) {
  54.         q = Selector[z];
  55.  
  56.  
  57.  
  58.        switch(q) {
  59.                case 'A' :
  60.  
  61.                     Mac1Array [a] = Selector[z];
  62.                                           a++;
  63.                           break;
  64.             case 'B' :
  65.                     Mac2Array [b] = Selector[z];
  66.                                         b++;
  67.               break;
  68.             case 'C' :
  69.                     Mac3Array [c] = Selector[z];
  70.                                         c++;
  71.               break;
  72.             case 'D' :
  73.                                         d++;
  74.                     Mac4Array [d] = Selector[z];
  75.  
  76.               break;
  77.             default:
  78.             break;
  79.  
  80.        }
  81.         z++;
  82.     }
  83.     printf("THE VALUES IN MACHING ONE IN ORDER OF ARIVAL ARE:\n");
  84.     for (i=0; i < 20; i++)
  85.         printf("%c\t", Mac1Array[i]);
  86.     printf("THE VALUES IN MACHING TWO IN ORDER OF ARIVAL ARE:\n");
  87.     for (i=0; i < 20; i++)
  88.         printf("%c\t", Mac2Array[i]);
  89.     printf("THE VALUES IN MACHING THREE IN ORDER OF ARIVAL ARE:\n");
  90.     for (i=0; i < 20; i++)
  91.         printf("%c\t", Mac3Array[i]);
  92.     printf("THE VALUES IN MACHING FOUR IN ORDER OF ARIVAL ARE:\n");
  93.     for (i=0; i < 20; i++)
  94.         printf("%c\t", Mac4Array[i]);
  95.  
  96.     return 0;
  97. }
  98.  
  99.  
  100.  
  101. int main(void)
  102. {
  103.     InputSelector();
  104.     return 0;
  105. }
Aug 23 '11 #1

✓ answered by weaknessforcats

aRSG is a local variable. As such, it will be destroyed whn you leave te first function. To preserve it, allocate te array on heap using malloc(). Then have the function either a) return the array address or b) have a second argument tha is the address of a pointer of the array type. The first function can then place the address of the array in th pointer address passed in.

You will also need a companion variable that contains the number of elements. This companion is passed around as needed. In C, when you only know the array address, the array appears as a pointer to element 0 and the number of elements is lost. This is called decay of array and is something you need to be aware of.

1 2621
weaknessforcats
9,208 Expert Mod 8TB
aRSG is a local variable. As such, it will be destroyed whn you leave te first function. To preserve it, allocate te array on heap using malloc(). Then have the function either a) return the array address or b) have a second argument tha is the address of a pointer of the array type. The first function can then place the address of the array in th pointer address passed in.

You will also need a companion variable that contains the number of elements. This companion is passed around as needed. In C, when you only know the array address, the array appears as a pointer to element 0 and the number of elements is lost. This is called decay of array and is something you need to be aware of.
Aug 24 '11 #2

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

Similar topics

2
by: CES | last post by:
All, Sorry for the 101 question but I can't figure out how to return all of the contents of an array to another function. I been able to figure out how to return individual elements of the array...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
2
by: Tobias Olbort | last post by:
Hello, i've a outer function, which takes a params-array as a parameter. I want to pass this array to inner function with a params-array (e. g. string.format). When i've passed an integer to...
3
by: Steve | last post by:
How can I copy 5 bytes from the middle of one array to another in Managed C++? The following code segment causes the compilation errors below: unsigned char cResult __gc = new unsigned char __gc;...
6
by: Soumyadip Rakshit | last post by:
I have a 2D Array of Integers A. I would like to copy it to another array B taking each row at a time. They are both initialized as pointers to pointers. I would like to use something like the...
0
by: DWalker | last post by:
VBA, as used in Excel 2000 and Excel 2003, has a function called Array. It's commonly seen in statements like the following: Workbooks.OpenText Filename:=ACFileName, _ StartRow:=1,...
26
by: MLH | last post by:
How would I modify the following to achieve a 2-dimensional array? Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") ' Return values assume lower bound set to 1...
3
by: unicorn7777777 | last post by:
Hi, I need a deep copy constructor for a class which contains an array for another class: class Chessboard { public: ChessSquare chessSquare; // copy constructor needed to copy all...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
1
by: aspire | last post by:
All, I am trying to allocate memory to a pointer to an array in another function, but i am not getting a successful compilation. I am getting error on a line shown below in code. ------------...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.