admin管理员组文章数量:1332353
I have 2 ip addresses of the form xxx.xxx.xxx.xxx
stored as 4 integers xxx
(0-255) in a structure.
I have successfully converted the integers to strings, and backfilled with '0' so I can compare the two.
i.e. 1 => 001 , 96 => 096
However, I am having trouble storing the string to my structure when exiting my function and cannot see my error as everything is defined.
My code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ipToStr{
char ip1Str [4];
char ip2Str [4];
char ip3Str [4];
char ip4Str [4];
};
void backfillZero(int ip, char *saveTo){
char tempIp [4];
char zeros [4];
sprintf(tempIp, "%i", ip);
int nDigits = strlen(tempIp);
while(nDigits < 3){
strcat(zeros, "0");
nDigits += 1;
}
strcat(zeros, tempIp);
printf("Backfiled: %s\n",zeros);
strcpy(saveTo, zeros); //causes error
}
void main(){
char filledIp [13]; //after calling this function 4 times, I'll use strcat and
//this string to store the whole ip, then conver to int
struct ipToStr *backfilledIp;
backfillZero(12, backfilledIp->ip1Str);
printf("ip: %s\n", backfilledIp->ip1Str);
}
I have 2 ip addresses of the form xxx.xxx.xxx.xxx
stored as 4 integers xxx
(0-255) in a structure.
I have successfully converted the integers to strings, and backfilled with '0' so I can compare the two.
i.e. 1 => 001 , 96 => 096
However, I am having trouble storing the string to my structure when exiting my function and cannot see my error as everything is defined.
My code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ipToStr{
char ip1Str [4];
char ip2Str [4];
char ip3Str [4];
char ip4Str [4];
};
void backfillZero(int ip, char *saveTo){
char tempIp [4];
char zeros [4];
sprintf(tempIp, "%i", ip);
int nDigits = strlen(tempIp);
while(nDigits < 3){
strcat(zeros, "0");
nDigits += 1;
}
strcat(zeros, tempIp);
printf("Backfiled: %s\n",zeros);
strcpy(saveTo, zeros); //causes error
}
void main(){
char filledIp [13]; //after calling this function 4 times, I'll use strcat and
//this string to store the whole ip, then conver to int
struct ipToStr *backfilledIp;
backfillZero(12, backfilledIp->ip1Str);
printf("ip: %s\n", backfilledIp->ip1Str);
}
Share
Improve this question
asked Nov 20, 2024 at 22:57
Aman HAman H
155 bronze badges
11
|
Show 6 more comments
1 Answer
Reset to default 2Main problem:
struct ipToStr *backfilledIp;
backfillZero(12, backfilledIp->ip1Str);
backfilledIp
pointer is not initialized and it is not referencing valid memory. It is undefined behaviour.
- instead of this weird function
backfillZero
simply dosprintf(saveTo, "%03d", ip);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ipToStr{
char ip1Str [4];
char ip2Str [4];
char ip3Str [4];
char ip4Str [4];
};
void backfillZero(int ip, char *saveTo, size_t size)
{
snprintf(saveTo, size, "%03d", ip);
}
void main(){
char filledIp [13]; //after calling this function 4 times, I'll use strcat and
//this string to store the whole ip, then conver to int
struct ipToStr backfilledIp;
struct ipToStr *backfilledIp1 = malloc(sizeof(*backfilledIp1));
backfillZero(12, backfilledIp.ip1Str, sizeof(backfilledIp.ip1Str));
printf("ip: %s\n", backfilledIp.ip1Str);
if(backfilledIp1)
{
backfillZero(25, backfilledIp1 -> ip1Str, sizeof(backfilledIp1 -> ip1Str));
printf("ip: %s\n", backfilledIp1 -> ip1Str);
}
free(backfilledIp1);
}
https://godbolt./z/4fKehajnM
本文标签: cstrcpy with struct string returning errorStack Overflow
版权声明:本文标题:c - strcpy with struct string returning error - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322996a2453164.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
zeros
to an empty string before you can usestrcat()
on it. – Barmar Commented Nov 20, 2024 at 23:00sprintf(tempIp, "%03d", ip);
– Barmar Commented Nov 20, 2024 at 23:04backfilledIp
doesn't point anywhere. – dbush Commented Nov 20, 2024 at 23:06