The problem here is that you tell the printf function that you pass it a signed integer, but what you actually pass is a unsigned 8-byte integer, so when it advances to the next argument, it interprets the next four bytes as a pointer to a string, which is zero and it prints exactly that: (null).
So there's a couple of ways you can approach this:
1) use simple unsigned long and use %u modifier to print it (why do you need long long anyway?)
2) change to type-safe c++ style handling of streams (better option):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <string>
struct student
{
unsigned long long id;
std::string name;
};
int main()
{
student msteel;
msteel.id = 34443215;
msteel.name = "kiel";
std::cout << msteel.id << "\n" << msteel.name << std::endl;
return 0;
}
|
If you wish to stick to C-style, there's one more thing for you to note.
When you use a literal string (such as "kiel") in code, the memory for it is allocated inside the executable and is loaded to memory with it. So when you allocate a pointer
msteel.name
and then assign "kiel" to it, you effectively lose the original allocated address (and a free call will fail).
So, again, you have two options:
1) use the address of the literal string "kiel" and avoid any allocations by simply removing lines 14 and 17
2)
copy the string from the executable-preallocated memory to the address you allocated. Then instead of line 15 you should write (don't forget to include string.h):
strcpy(msteel.name, "kiel");
However, I really recommend to use type-safe C++ style.