Reputation: 1004
In the code below i was expecting another output! :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _cust {
char customerId[10];
char customerPhone[10];
char customerDep[4];
} cust;
int main(int argc, char **argv) {
cust *newCust;
const char testChar[] = "11W35A5CT-012345678-CORP";
newCust = (cust *)malloc(sizeof(struct _cust));
newCust = (cust *)testChar;
printf("CustomerId = %s\n", newCust->customerId);
printf("CustomerPhone = %s\n", newCust->customerPhone);
printf("CustomerDep = %s\n", newCust->customerDep);
return 0;
}
And the output is :
CustomerId = 11W35A5CT-012345678-CORP
CustomerPhone = 012345678-CORP
CustomerDep = CORP
I was expecting this output :
CustomerId = 11W35A5CT-
CustomerPhone = 012345678-
CustomerDep = CORP
Can someone explain to me why this? Thanks.
EDIT :
To avoid confusing on my post i'm adding here the gdb trace when debugging this program:
(gdb) b main
Breakpoint 1 at 0x8048474: file name.c, line 11.
(gdb) run
Starting program: /home/evariste/src/customer_files/a.out
Breakpoint 1, main (argc=1, argv=0xbffff2c4) at name.c:11
11 int main(int argc, char **argv) {
(gdb) n
13 const char testChar[] = "11W35A5CT-012345678-CORP";
(gdb) n
15 newCust = (cust *)malloc(sizeof(struct _cust));
(gdb) n
16 newCust = (cust *)testChar;
(gdb) n
21 printf("CustomerId = %s\n", newCust->customerId);
(gdb) print *newCust
$1 = {customerId = "11W35A5CT-", customerPhone = "012345678-",
customerDep = "CORP"}
So whey here i see that customerId = "11W35A5CT-" and when i try printf i got the whole string?
Upvotes: 0
Views: 5064
Reputation: 33437
This is a bit of an over simplification, but hopefully it will illustrate what's happening.
typedef struct _cust {
char customerId[10];
char customerPhone[10];
char customerDep[4];
} cust;
What the compiler will do with this is pretend it's a 24 character blob. Assume that blob is rooted at a char* called x. Then customerId is just an alias for x+0, customerPhone is an alias for x + 10, and customerDep an alias for x + 20.
So, what happening when you over write the pointer with the string is that x = testChar.
That means customerId is &testChar[0], customerPhone is &testChar[10] and so on.
The problem with this is that printf and friends, as QuantumMechanic said, do not have a concept of a range for a string. A C string is just beginning until null byte. That means that printf() on customId will go from testChar[0] until it hits a null byte which will be the end of the testchar string.
To do what you want, each string will have to have a null byte inserted at the end of it, meaning your overlay like this not going to work (also I believe your overlay could result in undefined behavior, but I'm not sure).
(Or you could do like mteckert suggested.)
Upvotes: 2
Reputation: 1975
Ditch the malloc
, specify the size of the string to print in the printf
format specifier.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _cust {
char customerId[10];
char customerPhone[10];
char customerDep[4];
} cust;
int main(int argc, char **argv) {
cust *newCust;
const char testChar[] = "11W35A5CT-012345678-CORP";
newCust = (cust *)testChar;
printf("CustomerId = %.*s\n",
sizeof(newCust->customerId), newCust->customerId);
printf("CustomerPhone = %.*s\n",
sizeof(newCust->customerPhone), newCust->customerPhone);
printf("CustomerDep = %.*s\n",
sizeof(newCust->customerDep),newCust->customerDep);
return 0;
}
Upvotes: 0
Reputation: 13946
printf()
will output until it hits a \0
which signals the end of the string. There's no \0
after any of the hyphens and so printf()
will print from the start position you gave it to the end of what's in testChar
.
Also, you leaked away the memory the call to malloc
allocated for you. Perhaps you want to copy the string into the struct?
Upvotes: 6