简体   繁体   中英

Printing a jagged array in c

so I'm working on making a jagged array in C. I think my code to fill the array is okay, what I'm having issues with is the printing.

Code below

#include <stdio.h>

int *length;
int **row;

int main(int argc, char *argv[]) {

    //make an array that contains the lengths of the columns for each row
    length = malloc(argc * sizeof (int));
    for (int k = 0; k < argc; k++) {
        length[k] = atoi(argv[k]);
       // printf("%d ", lengths[k]);
    }
    row = malloc(argc * sizeof (int));

    // fill the columns
    int fill = 1;
    for (int i = 0; i < argc; i++) {
        row[i] = malloc(sizeof (int) * length[i]);
        for (int j = 0; j < length[i]; j++)
            row[i][j] = fill;

    }

    //print it
    for (int i = 0; i < argc; i++)
        for (int j = 0; j < length[i]; j++)
            printf("%d", row[i][j]);

    return 0;


}

This program in takes command line arguments, so if I input:

./jagged 1 3 5 1

I should get:

1
1 1 1
1 1 1 1 1
1

Instead my compiler just says

RUN FAILED

argc == 5 , and argv[0] contains ./jagged .

atoi will fail for argv[0] , and its error behavior is undefined. If the program continues, 0 is the value returned, and you'll store 0 as one of the lengths. This means you'll be executing malloc(sizeof(int) * 0); which may also cause trouble.

To combat these issues, you have two options:

  1. Loop from i = 1; i < argc; i++ i = 1; i < argc; i++ i = 1; i < argc; i++ to avoid argv[0] .
  2. Add a line --argc, ++argv; before you use either of argc or argv .

You should also consider using strtol instead of atoi as it has well-defined error behavior.

Other than your program logic, you simply forgot to include <stdlib.h> to be able to use malloc and atoi as @Vishal explained .

Use some restrictive compiler flags, eg --pedantic --std=c11 -Wall -Wextra for gcc. This will help you to find some errors like missing includes yourself.

Include stdlib.h for your call of malloc() .

argv[0] is not your first command line argument, it is the name of your binary. So you have to adapt the loops.

To simplify the case that no arguments were given, include assert.h and check the number of arguments at the beginning of your main with assert(argc > 1); .

Your allocation of row is not correct but plattform dependant, since the elements of row are of type int * and not int . Instead allocate row = malloc((argc - 1) * sizeof(int *)); .

you need to include stdlib to use malloc function and print \\n after first block

    #include <stdio.h>
    #include <stdlib.h>
    int *length;
    int **row;

    int main(int argc, char *argv[]) {

        //make an array that contains the lengths of the columns for each row
        length = malloc(argc * sizeof (int));
        for (int k = 0; k < argc; k++) {
            length[k] = atoi(argv[k]);
           // printf("%d ", lengths[k]);
        }
        row = malloc(argc * sizeof (int));

        // fill the columns
        int fill = 1;
        for (int i = 0; i < argc; i++) {
            row[i] = malloc(sizeof (int) * length[i]);
            for (int j = 0; j < length[i]; j++)
                row[i][j] = fill;

        }

        //print it
        for (int i = 0; i < argc; i++)
        {
            for (int j = 0; j < length[i]; j++){
                printf("%d", row[i][j]);
            }
            printf("\n");
        }

        return 0;


    }

Sources of Error

1> argv[0] is bound to fail.

2> No library included for malloc (need stdlib.h for that).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM