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
  • 3 You need to initialize zeros to an empty string before you can use strcat() on it. – Barmar Commented Nov 20, 2024 at 23:00
  • Is there a reason why you're not using sprintf's built-in method for adding leading zeroes? – Barmar Commented Nov 20, 2024 at 23:00
  • 3 sprintf(tempIp, "%03d", ip); – Barmar Commented Nov 20, 2024 at 23:04
  • 3 backfilledIp doesn't point anywhere. – dbush Commented Nov 20, 2024 at 23:06
  • 2 @Barmar wouldn't you just use an unsigned 32-bit int? – jarmod Commented Nov 20, 2024 at 23:46
 |  Show 6 more comments

1 Answer 1

Reset to default 2

Main problem:

    struct ipToStr *backfilledIp;
    backfillZero(12, backfilledIp->ip1Str);

backfilledIp pointer is not initialized and it is not referencing valid memory. It is undefined behaviour.

  1. instead of this weird function backfillZero simply do sprintf(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