-2
double** makeit(int, int);
void showit(double**, int, int, int);

int main()
{
    int i,j;
    int x,y;
    printf("x=");
    scanf("%d",&x);
    printf("y=");
    scanf("%d",&y);

    double (*mas2d)[x];
    mas2d=makeit(x,y);
    printf("%4.0f ir  %4.0f \n",mas2d[0][0],mas2d[1][0]);
    showit(&mas2d, x, y);

    return 0;
}

double** makeit(int x, int y)
{
    double (*masp)[x];
    int i,j;
    double skc;
    masp= malloc((x*y)*sizeof(double));
    skc=1;

    for (i=0;i<x;i++)
    {
        for (j=0;j<y;j++)
        {
            masp[i][j]=skc;
            skc++;
        }
    }
    return masp;
}

void showit(double** mas[], int x, int y)
{
    int i,j;
    printf("%4.0f ir  %4.0f \n",mas[0][0],mas[1][0]);
    printf("x===%d",x);

    for(i=0;i<x;i++)
    {
        printf("\n");

        for(j=0;j<y;j++) 
        {
            printf("%4.0f \n",mas[i][j]);
        }
    }
}

What I do 1. I dynamically allocate double array mas2d in function makeit. 2. I want to send that mas2d array pointer to function showit and print it there.

What's the problem I can print mas2d array pointer from main function with no problems, but when I pass it to separate function showit, I just cant get it work... I've been trying to send it as a 3D pointer and maybe 100 other ways with no luck at all.

5
  • 1
    Here you call show like this showit(&mas2d, x, y); but you declare it like this =>> void showit(double**, int, int, int); , Why ? Commented May 27, 2016 at 17:50
  • 2
    Where are makeit() and showit()? Commented May 27, 2016 at 17:53
  • Push up your compiler's warning level and fix the code until no more warnings are issued. Commented May 27, 2016 at 17:56
  • @rokastokas Take a look Here maybe will help somehow. The way I see you need a Function returning a Pointer to an Array Commented May 27, 2016 at 17:58
  • @alk, sorry i forgot to change those names from my language, fixed. Commented May 27, 2016 at 19:17

2 Answers 2

1

The cleanest way to do this is to have all instances of the 2D use the type double **. When you do your allocations, you'll first need to allocate an array of x double *, then for each row allocate y double.

double** makeit(int x, int y){
    double **masp;
    int i,j;
    double skc;

    masp= malloc(x*sizeof(double *));
    skc=1;

    for (i=0;i<x;i++) {
        masp[i]= malloc(y*sizeof(double));
        for (j=0;j<y;j++){
            masp[i][j]=skc;
            skc++;
        }
    }

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

1 Comment

thank you!! i understand allocating your way is proper way, but still, is it possible to do this in my way? maybe wrong passing, or smtg?
0

Try this (see details on changes as comments to the source):

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

double (*makeit(size_t x, size_t y))[] /* Make it return what it creates,
                                                      a pointer to an array of double.  */
{
  double (*masp)[x];
  size_t i, j; /* Index arrays and address storage using size_t. */
  double skc = 1.; /* Always initialise on definition if possible; Use a double literal 
                                                                to initialise a double. */
  masp = malloc(y * sizeof *masp); /* Allocate y times what masp points to,
                                                         that is an array of x doubles. */
  if (NULL == masp) /* Return is we got nothing. */
  {
    return NULL;
  }
  /* skc = 1; */ /* Not needed any more (see definition of skc). */

  for (i = 0; i < x; i++)
  {
    for (j = 0; j < y; j++)
    {
        masp[i][j] = skc;
        skc++;
    }
  }
  return masp;
}

void showit(size_t x, size_t y, double (*mas)[x]) /* Pass the address of a VLA
                                              after passing it primary dimension (x). */
{
  size_t i, j; /* Index arrays and address storage using size_t. */
  printf("%4.0f ir  %4.0f \n",mas[0][0], mas[1][0]);
  printf("x===%zu", x); /* Use the appropriate conversion specifier for size_t */

  for (i = 0; i < x; i++)  
  {
    printf("\n");

    for (j = 0; j < y; j++)
    {
        printf("%4.0f \n", mas[i][j]);
    }
  }
}

int main(void) /* It ought to be this at least. */
{
  //int i, j; /* Unused. */
  size_t x,y; /* Index array and address storage using size_t. */

  printf("x=");
  fflush(stdout); /* Make sure the output is shown. */
  scanf("%zu",&x); /* Use the appropriate specifier for size_t */

  printf("y=");  /* Make sure the output is shown. */
  fflush(stdout); /* Use the appropriate specifier for size_t */
  scanf("%zu",&y); /* Make sure the output is shown. */

  double (*mas2d)[x] = makeit(x, y); /* Always initialise on definition if possible. */

  if (NULL == mas2d) /* Do error checking! */
  {
    perror("makeit() failed");
    exit(EXIT_FAILURE);
  } 

  printf("%4.0f ir  %4.0f \n", mas2d[0][0], mas2d[1][0]); 

  showit(x, y, mas2d); /* Adjust order of parameters suiting the changes. */

  return 0;
}

Comments

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.