6

I've tried using a tripple pointer, but it keeps failing. Code:

#include <stdlib.h>
#include <stdio.h>

int set(int *** list) {
  int count, i;
  printf("Enter number:\n");
  scanf("%d", &count);
  (*list) = (int **) malloc ( sizeof (int) * count);

  for ( i = 0; i<count;i++ ) {
    (**list)[count] = 123;
  }
  return count;
}

int main ( int argc, char ** argv )
{
  int ** list;
  int count;

  count = set(&list);

  return 0;
}

Thanks for any advice

8
  • 7
    I made myself a rule long ago that if my code has *** anywhere I have to rewrite it. Commented Nov 3, 2012 at 11:25
  • I've heard of this rule, but in this case, *** will save creating a function that would be called only once. Commented Nov 3, 2012 at 11:28
  • No, it doesn't mean recoding, it means redesigning your data structures so that you don't need all those layers of indirection. Commented Nov 3, 2012 at 11:31
  • 1
    yOu should use (**list)[i]=123 instead of (**list)[count]=123 Commented Nov 3, 2012 at 11:39
  • 1
    Obligatory "three star programmer" joke: webcache.googleusercontent.com/… Commented Oct 16, 2016 at 17:32

3 Answers 3

8

What you call list is actually an array. You might do it the following way:

#include <stdlib.h>
#include <stdio.h>

ssize_t set(int ** ppList) 
{
  ssize_t count = -1;

  printf("Enter number:\n");
  scanf("%zd", &count);

  if (0 <= count)
  {
    (*ppList) = malloc(count * sizeof **ppList);

    if (*ppList)
    {
      size_t i = 0;
      for (; i < count; ++i)
      {
        (*ppList)[i] = 42;
      }
    }
    else
    {
      count = -1;
    }
  }

  return count;
}

int main (void)
{
  int * pList = NULL;
  size_t count = 0;

  {
    ssize_t result = set(&pList);

    if (0 > result)
    {
      perror("set() failed");
    }
    else
    {
      count = result;
    }
  }

  if (count)
  {
    /* use pList */
  }

  ...

  free(pList);

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

Comments

4

As far as I understand your question you want to return an array which is allocated in another function : here is the simple version of this

#include<stdio.h>
#include<stdlib.h>
int* set(int *list) {
  int count, i;
  printf("Enter number:\n");
  scanf("%d", &count);
  list = (int *) malloc ( sizeof (int) * count);

  for ( i = 0; i<count;i++ ) {
    list[i] = 123;
  }

  return list;
}

int main ( int argc, char ** argv )
{
  int *list;
  list = set(list);
  //Use whatever you want to do with that array 
  free(list); // don't forget to free
  return 0;
}

1 Comment

I have not checked if malloc fails condition
1

you have an array of integer arrays. Let's look at your set function closely:

for (i = 0; i < count;i++ ) {
    (**list)[count] = 123;
}

As you can see you are treating every array object like an integer value. That should be a nested loop:

for (i to n)
    // allocate each array
    for (k to m)
        // assign value for each value of array

4 Comments

Oh, I didn't see that. But the question is: How to allocade 1-dimensional array and return it using reference?
@Grant by reference do you mean return &array or what?
By using reference I mean as an output parameter.
@Grant for (i to n)<br/> (**list)[i] = new int[desired_length_of_each_array]<br/> for (k to m)<br/> (*list)[k] = desired_value<br/> But i assume you are doing this to learn the c syntax. Because it's not a good programming style to use a tripple pointer, since the syntax is very confusing. of the topic: By the way i am new to this forum and i would like to learn how to enter a new line in a comment.

Your Answer

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