0

I just wrote this snippet of code and have passed values of integers in for when it scans the integer in, but am getting back the memory address of the int towards the end.. how do I display only the number that I just read in instead of it's address? Can I simplify this code snippet even more?

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

typedef struct building {

    char *blockName;
    int blockNumber;

} building;

int main() {

    building *blockA = (building*)malloc(sizeof(building));
    building *blockB = (building*)malloc(sizeof(building));
    blockA->blockName = (char*)malloc(25*sizeof(char*));
    blockB->blockName = (char*)malloc(25*sizeof(char*));
    blockA->blockNumber = (int)malloc(sizeof(int));
    blockB->blockNumber = (int)malloc(sizeof(int));

    printf("What is the name for your first block: ");
    scanf("%s", (*blockA).blockName);
    printf("What will be it's number: ");
    scanf("%d", (*blockA).blockNumber);

    printf("\n");

    printf("What is the name for your second block: ");
    scanf("%s", (*blockB).blockName);
    printf("What will be it's number: ");
    scanf("%d", (*blockB).blockNumber);

    printf("\n");

    printf("Your first block's name is %s. It's number is %d\n", (*blockA).blockName, (*blockA).blockNumber);
    printf("Your second block's name is %s. It's number is %d\n", (*blockB).blockName, (*blockB).blockNumber);

    printf("\n");

    free(blockA->blockName);
    free(blockB->blockName);
    free(blockA);
    free(blockB);

    system("pause");

    return 0;

}
M Oehm
  • 28,726
  • 3
  • 31
  • 42
LatentDenis
  • 2,839
  • 12
  • 48
  • 99
  • you didn't put values in your variables, so they have either trash or 0. moreover, don't allocate integers. – ThunderWiring Jun 02 '15 at 05:13
  • 4
    [Don't cast malloc](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). Here the cast is hiding an error message that is important to read. – M.M Jun 02 '15 at 05:24

2 Answers2

4

The member

int blocknumber;

is a plain scalar integer, not a pointer. Use it directly, without allocating memory to it. This line:

blockA->blockNumber = (int)malloc(sizeof(int));

is very suspicious and your compiler should have warned you. (You do compile with warnings enabled, don't you?) You are tyring to store a pointer in an integer value, which will fail on machines where the size of a pointer is greater then the size of an int.

The remedy is not to allocate memory for a scalar, then

scanf("%d", &(*blockB).blockNumber);

(note the &), and then you will have the user input available as:

printf("It's number is %d\n", (*blockB).blockNumber);

On the other hand, the malloc for the string is right, because the string is an array of chars, in this case allocated on the heap and represented by a pointer to the first char.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • (Matt McNabb is right: Of course the compiler didn't warn you, because you told it to be silent with an explicit cast.) – M Oehm Jun 02 '15 at 05:33
2

Since building is defined as

typedef struct building {
    char *blockName;
    int blockNumber;

} building;

You shouldn't be doing this

blockA->blockNumber = (int)malloc(sizeof(int));

as the line building *blockA = (building*)malloc(sizeof(building)); will already have allocated space on the heap for the int blockNumber:

You can simply assign it

blockA->blockNumber = 1234;

Or prompt the user for it

 scanf("%d", &(blockA->blockNumber));
StuartLC
  • 104,537
  • 17
  • 209
  • 285