d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hepl Array And Function [c]
Add Reply New Topic New Poll
Member
Posts: 21,154
Joined: Apr 2 2006
Gold: 4,501.00
Jul 2 2012 09:29am
Im programming on c,
i need to receive and array and use it on a function..

i've tried with

int arrayfunc(int arrayx[10])
{
BLA BLA BLA
return (0);
}

int main()
{
int array[10], x;
for (x=0; x<10; x++)
{
printf("Introduce the numbers");
scanf("%i", &array[x]);
}
arrayfunc(array[10);



return (0);
}

i just need the numbers of the array to enter the function.. any help is really appreciated
Member
Posts: 16,144
Joined: Mar 27 2008
Gold: 14,618.00
Jul 2 2012 12:31pm
you only need to send the address of the array to your function. the function goes then to the address and gets the data:
Code
int arrayfunc(int *arrayx){ ...}

the star says that it's a pointer to an int (and an array is just a bunch of integers, where the variable name "array" is therefore a pointer)
you can call your function with different possibilities. I would just write:
Code
arrayfunc(&array);

where the ampersand just says that you take the address of the pointer

if you want you can also send the length of the array to the function (add another variable)
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 2 2012 12:35pm
Pass q pointer pointing to the array.


Your function would look like

Int functionname(int *array)

Then you would use the array like normal inside the function

Array[x] = 10;

To call the function you just do

Functioname(array)
Member
Posts: 21,154
Joined: Apr 2 2006
Gold: 4,501.00
Jul 3 2012 11:00am
thanks alot.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll