AlexL AlexL - 7 months ago 97
C Question

Null pointer passed as argument 1

In line

int d = atoi(argv[2]);
there seems to be something wrong. "null pointer passed as argument 1..."

What can I do?

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

int main(int argc, string argv[])
{

int num;
int i = 1;
int j = 1;
int board [i][j];

// 2nd CL argument is size of grid.

if (argc != 2)
{
return false;
}

int d = atoi(argv[2]);

if (d <= 0)
{
return false;
}

// setting up board.

// number of tiles needed.
num = d * d - 1;

// iterating over columns and rows
for (i = 0; i <= d; i++)
{
for (j = 0; j <= d; j++)
{
// set values.
board[i][j] = num;
num --;
}
printf ("%d", board[i][j]); // TESTING.
printf ("\n");
}

// if even swap 1 & 2. Don't display 1.
}

Answer Source

argv[0] holds the name of the command used to start your program. If you expect two command line arguments, check argc == 3 and read the command line arguments from argv[1] and argv[2].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download