user3108849
user3108849

Reputation: 111

Thrown exception: Access violation writing location 0x00000000

I keep getting the following error for the following code segment. I don't know what it means. Can someone tell me why I keep getting it?

Unhandled exception at 0x5A0DB49C (msvcr110d.dll) in ConsoleApplication8.exe: 0xC0000005: Access violation writing location 0x00000000.

#include <stdio.h>
#include <string.h>

int main ( void )
{
    char str[]="asd asd 3, 5; 12, 525; 123, 45235;";
    int a[100]={0};
    int b[100]={0};
    int i=0;
    sscanf (str, "asd asd %d, %d; %d, %d; %d, %d;", a[0], b[0], a[1], b[1], a[2], b[2]);

    for (i=0; i<3; i++){

        printf ("%d %d\n", a[i], b[i]);

    }
    return 0;

}

Upvotes: 2

Views: 3608

Answers (2)

haccks
haccks

Reputation: 105992

Syntax for sscanf

sscanf(characterArray, "Conversion specifier", address of variables); 

suggest that, you need & here in sscanf argument for int type.

sscanf (str, "asd asd %d, %d; %d, %d; %d, %d;", a[0], b[0], &a[1], &b[1], &a[2], &b[2]);

Upvotes: 3

ldav1s
ldav1s

Reputation: 16305

Try:

sscanf (str, "asd asd %d, %d; %d, %d; %d, %d;", &a[0], &b[0], &a[1], &b[1], &a[2], &b[2]);

instead of the sscanf call you've got. Otherwise, you're probably going to get a null pointer exception (int array values are 0).

Upvotes: 0

Related Questions