Reputation: 107
char* dayOfProgrammer(int year) {
char s[15];
if (year >= 1700 && year < 1918) {
if (year % 4 == 0) {
strcpy(s,"12.09.");
strcat(s,(char*)(intptr_t)year);
} else {
strcpy(s,"13.09.");
strcat(s,(char*)(intptr_t)year);
}
}
if (year >1918 && year < 2700) {
if(year%400==0 || (year%4==0 && year%100!=0)){
strcpy(s,"12.09.");
strcat(s,(char*)(intptr_t)year);
}else{
strcpy(s,"13.09.");
strcat(s,(char*)(intptr_t)year);
}
}
if(year==1918){
strcpy(s,"26.09.");
strcat(s,(char*)(intptr_t)year);
} return s; }
I have tried ever thing to take output but its showing error
0x00007fc49d95dc85 in __strcpy_chk (dest=0x7ffed426cc9f "",
src=0x7e1 <error: Cannot access memory at address 0x7e1>, destlen=15)
at strcpy_chk.c:28
my task is to add pointer char and number like, ("12.09."+some number).
If you have any method then please suggest. I have try memory alloaction also but it also didn't work. I have found very diffculties in joining to pointer string and int
Upvotes: 0
Views: 3345
Reputation: 2823
strcpy(s,"12.09.");
strcat(s,(char*)(intptr_t)year);
This is most likely not doing what you think it does. You cast the year to an address and try to access it which in nearly every case invokes undefined behavior. You should take a look at the snprintf
function which is suitable for this task:
snprintf(s, sizeof s, "12.09.%d", year);
The next problem is that you are trying to return a local array which also leads to undefined behavior. You have to either make it static
or allocate it with malloc
.
Upvotes: 2