1

I have a function which creates a dynamic array the size of my selected integer. Code:

int *create(int n) {
    int *nn;
    nn = (int*)malloc(n*sizeof(int));
    return nn;
}

And I call it like this in my main()

int *nn
int n = 5; /* size = 5 */
nn = create(n);

I think I get the part, int *create(...) which is supposed to return the address to the first position of my returned nn. However I wonder, is there a way to not use the pointer in a function and instead modify the return nn; part so that I still return the address of the dynamic array nn?

I want to remove the * in the *create

16
  • return (int*) malloc(n*sizeof(int)) ? Commented Oct 14, 2015 at 17:59
  • 1
    There are no function pointers in this code... Commented Oct 14, 2015 at 18:00
  • 1
    Also, the canonical way to do this would just be int * nn = malloc(n*sizeof(int)); You don't cast malloc in c. So the example is a little confusing. Commented Oct 14, 2015 at 18:01
  • 1
    What is your reason for wanting eliminating the *? It's possible but it ain't pretty. Commented Oct 14, 2015 at 18:11
  • 1
    Why do you want to remove the * from the create function declaration/definition? What is your justification? Do you mean that you don't want the function to return a pointer value? If not, why not? Commented Oct 14, 2015 at 18:11

2 Answers 2

5

You can modify your code to set a pointer passed into your function by address, like this:

void create(int n, int** res) {
    *res = malloc(n*sizeof(int));
}

Here is how you call this function now:

int *nn
int n = 5;
create(n, &nn);

I want to use only one parameter and be able to do it with a return.

You can return the result of malloc directly, since that is all your function does anyway:

int *create(int n) {
    return malloc(n*sizeof(int));
}

The call can be combined with the declaration as well:

int n = 5;
int *nn = create(n);

Of course if you do that, you might as well do the idiomatic

int *nn = malloc(n*sizeof(*nn));

and drop the create function altogether.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, that's one of the ways it's possible to solve it, however I want to use only one parameter and be able to do it with a return. Is it impossible to do so?
Isn't this over-egging the pudding? See comment above from @QuestionC.
"...and drop the create function altogether" is the right answer in this case, since create isn't doing anything other than calling malloc.
0

You could do :

int *create(int n) 
{
    return malloc(n*sizeof(int));
}

This way you don't need a temporary variable inside your create() function. But I don't see the point to make a function that does only a call to malloc.

If you want your function to return an array of integer, you need your function to be in the form int *create() since an array can be considered a pointer to its first element, that is an integer in your case.

1 Comment

I'm sorry for not clarifying one thing, what I want to do is remove is the * in the `*create*

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.