0

thanks for taking the time in reading this.
In my question a "vector" is defined as a 1D dimensional array of integers.
Therefore an array of vectors would be a 2D dimensional array in which every vector can be of a different length.
I'm asked to use:
int** vectors- the 2D array
int size -an integer that represents how many vectors exist inside **vectors
int* sizes-a 1D array of integers that represents the length of the vectors

for example,for:
vectors = {{4,3,4,3},{11,22,33,44,55,66},NULL,{5},{3,33,333,33,3}}.
size is 5 (there are 5 vectors inside vectors).
sizes is {4,6,0,1,5} (4 is the length of the first vector and so on).
size is inputted by the user at the beginning of main() and **vectors&*sizes are dynimacilly allocated with size's value.

I'm asked to write the function:
int init(int ***vectors, int **sizes, int size) which initializes **vectors to be an array of NULLs and *sizes to be an array of zeros.
I came up with this code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
    int i, k,j;
    printf("check\n");
    *vectors = (int**)malloc(size * sizeof(int*));
    if (*vectors == NULL)
        return 0;
    for (i = 0; i < size; i++)
    {
        *(vectors + i) = NULL;
    }
    printf("check 2\n");
    for (k = 0; k<size; k++)
    {
        if (*(vectors+k) != NULL)
            printf("didn't work\n");
        else
            printf("current is null\n");
    }
    *sizes= (int*)malloc(size * sizeof(int));
    if (*sizes == NULL)
        return 0;
    for (j= 0; j < size; j++)
    {
        *(sizes + j) = 0;
        printf("%d ", *(sizes + j));
    }
    printf("\n");
    return 1;
}
int main()
{
    int size, i;
    int** vectors = NULL;
    int* sizes = NULL;
    printf("\nPlease enter an amount of vectors:\n");
    scanf("%d", &size);
    printf("%d\n", init(&vectors, &sizes, size));
    printf("size is %d now\n", size);
//  for (i = 0; i < size; i++)
    //  printf("%d ", *(sizes+i));
    printf("check 3\n");
    free(sizes);
    free(vectors);
    printf("check 4\n");
    printf("check 5\n");
    return 0;
}

forgot to mention that init returns 0 if it fails to allocate memory and 1 otherwise.
printing the "checks" was so I could see where the program fails.
the problem is that no matter what,after printing the last check (check 5) the program fails.(Run-Time Check Failure #2)
if anyone could help me understand what I'm doing wrong I would HIGHLY appreciate it.
thanks alot for reading and have an amazing day. edit:
i also printed the array sizes/vectors inside init just to see if it prints zeros/nulls,i don't actually need to do it.

4
  • Don't be a Three Star Programmer. Oh, and a pointer to a pointer is not a 2d-Array ... Commented May 2, 2018 at 7:12
  • I was just introduced in class to pointers and my homework spesficially demands us to use three stars...sorry if it's dumb Commented May 2, 2018 at 7:15
  • Use warnings, for example ` warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=] printf("%d ", *(sizes + j));` Commented May 2, 2018 at 7:15
  • @Ultraviolence it's considered a code smell by many. of course you can make it work anyways -- just not a great idea, a different design would be better :) Commented May 2, 2018 at 7:25

1 Answer 1

1

One problem of OP's code is in the pointer arithmetic. Given:

int ***vectors;
*vectors = malloc(size * sizeof(int*));

This loop:

for (i = 0; i < size; i++)
{
    *(vectors + i) = NULL;
}

Would iterate over the next unallocated pointer to pointer to pointer to int, while what the OP needs is

for (i = 0; i < size; i++)
{
    *(*vectors + i) = NULL;     // or (*vectors)[i] = NULL;
}

The same holds in the following loops, where *(sizes + j) is used instead of *(*sizes + j) (or (*sizes)[j]).

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

1 Comment

thank you so much bob,this actually made the program not crash.can't thank you enough!

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.