0

This is what I want to do:

int randomtal(void){
  int loop;
  int tal[10];
  srand(time(0));

  for(loop = 0; loop < 10; loop++){
      tal[loop] = rand() % 10+1;
  }
  return tal;
}


int upp1(int argc, const char * argv[]) {
   int Tal[10];
   Tal[] = randomtal();
   return 0;
}

Simply the randomtal() function generates 10 numbers and put these in an array. I then want to pass this array to the upp1() function and put these in the Tal array. Basically make a copy of tal[] in function randomtal() and pass this to Tal in function upp1().

2
  • 1
    What is Tal[] = randomtal(); ? Commented Jan 25, 2018 at 14:39
  • Tal[] is the array I want to store all numbers in function upp1 and randomtal(); is me calling that function to generate numbers Commented Jan 25, 2018 at 14:40

1 Answer 1

2

You can change your function to accept an array as a parameter. And because an array as a function argument is actually a pointer to the first element of the array, changes to the array in the function are reflected in the caller.

void randomtal(int tal[10]){
  int loop;
  srand(time(0));

  for(loop = 0; loop < 10; loop++){
      tal[loop] = rand() % 10+1;
  }
}


int main(int argc, const char * argv[]) {
   int Tal[10];

   randomtal(Tal);

   return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.