0
votes

I'm writing this program in C that grabs contact information. I'm passing the pointer of a struct within a struct to a function that gets the address information, but when scanning the information into the pointer location I get a debug error.

Here's my code:

void getAddress(struct Address* address)
{
    // Street Number
    do  // Validate the street number
    {
        printf("Please enter the contact’s street number: ");
        scanf("%d", address->streetNumber); //<----where the error is
    } while (address->streetNumber <= 0);

    clearIS();    // Clears input stream

1
Pass a pointer to streetNumber, not the value of the variable: scanf("%d", &address->streetNumber); - Pablo
There's no assertion in the code you've shown. - jarmod
@jarmod it may be something inside the library implementation of scanf - M.M
@M.M Certainly looks that way and maybe it's as simple as address->streetNumber being zero. I'm surprised to see asserts active in a runtime library. - jarmod

1 Answers

1
votes

In short you should add &(address->streetName) and everything will work out.

The main reason for this behaviour is that scanf expect you to pass it a pointer to the place in memory where it should write an input.

In your current code you say to scanf to write 32 bits of user info into a memory that start with address from address->streetName and that's not what you want to do.

The same thing with address->streetName works fine because, I guess, you pass a pointer to array of chars to scanf. Because address->streetName is defined in your structure with type char* or char[N].