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!!
10 odpowiedzi
+ 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.
+ 9
Try this:
https://code.sololearn.com/cwrJPk93Bt3l
+ 5
@fds x is assigned the values of a, b, and c not addresses.
+ 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);
+ 4
@D_Stark the real question is: does arr[1] += 5 change y. If not, it isn't the same.
+ 4
@Ogbusgu here you go:
https://code.sololearn.com/WcEHD8otpQyU
+ 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.
+ 3
thanks John, I just saw it
+ 2
@ John wells, how is this done with JavaScript
+ 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