0

Can arrays take in variables instead of just values;

I was trying to figure out if a arrays can store variables or pointers is there away. or is there a function from that can be used to do this!!

9th Mar 2018, 2:43 AM
fds fds
fds fds - avatar
10 Respuestas
+ 5
A normal array cannot store addresses of variables, but can store their values. Eg - int arr[2],x=5; art[1]=x; Now, I don't understand how you intend to store variables (via their value or address), but to store pointers, you need a pointer array. Eg - int* ptr_arr[5]; // Pointer Array int* ptr, var=2; ptr=&val; ptr ++; ptr_arr[2]=&var; // Storing address of variable. ptr_arr[3]=ptr; // Storing address of variable // pointed by pointer. I am unable to understand what do you require a function for, so kindly elaborate on this if this is not what you wanted.
9th Mar 2018, 3:05 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
9th Mar 2018, 3:17 AM
John Wells
John Wells - avatar
+ 5
@fds x is assigned the values of a, b, and c not addresses.
10th Mar 2018, 1:27 AM
John Wells
John Wells - avatar
+ 4
You can set array values which in a way is an array of variables you can also assign the value to a new variable name if you wanted too. int arr[] = new int[10]; arr[0] = 123; arr[1] = 456; int y,x = arr[0]; y = arr[1]; System.out.println(x+""+y);
9th Mar 2018, 6:50 AM
D_Stark
D_Stark - avatar
+ 4
@D_Stark the real question is: does arr[1] += 5 change y. If not, it isn't the same.
9th Mar 2018, 12:14 PM
John Wells
John Wells - avatar
9th Mar 2018, 1:16 PM
John Wells
John Wells - avatar
+ 3
@D_Stark here is Java that works. https://code.sololearn.com/cXXEn91RQ2J7 @Ogbuago I'm guessing same as Java, but I'll let you know shortly. D_Stark commented on my code so I was already looking at his issue and failed to notice yours, until after posting. Sorry.
9th Mar 2018, 12:47 PM
John Wells
John Wells - avatar
+ 3
thanks John, I just saw it
28th Mar 2018, 9:50 AM
Ogbuagu Beloved Francis
Ogbuagu Beloved Francis - avatar
+ 2
@ John wells, how is this done with JavaScript
9th Mar 2018, 6:15 AM
Ogbuagu Beloved Francis
Ogbuagu Beloved Francis - avatar
+ 1
Thank you all @Kinshuk Vasisht I was trying to translate this python code to c++ code https://code.sololearn.com/cj14fIC0jsh1/#py And i was referring to a function from the standard library that could assign variables to a array lol It seems to me that learning python first was a bad idea
10th Mar 2018, 1:09 AM
fds fds
fds fds - avatar