0

This is probably a really stupid question, but

I have an array of structs outside of int main

typedef struct{
    char c;
    int k;
}factor_t;

and I declared

factor_t *factors = malloc(INIT*sizeof(*factors));

where INIT is 10

After running my function, I have an array of structs each which holds a char, c, and integer, k - e.g., factors[5].c could hold "b" or "d" or "e" and factors[5].k could hold "3" or "33" or "333"

I need to somehow insert these into a string, but I can't seem to

strcat(destination,c or k);

they both give me pointer to integer errors, destination is a char*

How would I go about putting these into a string? I'm aiming to get a string that looks like

ck
ck
ck

that is, a pattern of "ck\n" per struct, where c = char and k = integer

I use strcat(destination, "\n"); for the \n and it works, but I can't do the same with c and k

MikeCAT
  • 73,922
  • 11
  • 45
  • 70

4 Answers4

2

You can use sprintf to do so . -

size_t len=strlen(destination);         // calculate before concatenation
sprintf(&destination[len], "%c%d\n", factors[5].c,factors[5].k);   // string with newline

destination should be of type char *.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
2

Calculate the length of the string and output with that offset.

#include <stdio.h>
#include <string.h>

typedef struct{
    char c;
    int k;
}factor_t;

void struct_cat(char *str, factor_t f) {
    sprintf(str + strlen(str), "%c%d", f.c, f.k);
}

int main(void) {
    factor_t fac = {'b', 33};
    char buf[100] = "hogehoge";
    struct_cat(buf, fac);
    puts(buf);
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • be advised that `buf[100]` might not be big enough to store all values: 10 * a minimum of 3 chars (eg `'a'` + `'1'` + `'\n'`), with a max of 12 chars (`'a'` + `'2147483647'` (`INT_MAX`) + `'\n'`) -> you'll probably want `buf[121]` to be safe – Elias Van Ootegem Oct 16 '15 at 15:48
2

strcat appends a copy of the source string to the destination string. It expects c to be a null terminated string not a single char

If you want to add a single char to an array that is larger than n and the null terminating char is at index n

destination[n] = c;
destination[n+1] = '\0;

you have to be certain that destination is large enough.

If you want to format print additional data to destination string, again make sure destination is large enough and do :

sprintf(destination + n, "%c%d\n", c, k);

Or if you know how that destination has m chars left :

snprintf(destination + n, m, "%c%d\n", c, k);

With this if you attempt to print more than m chars the extra ones will be discarded.

Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
1

If you need separate this feature use function (like @MikeCAT). But use of snprintf() and strncat() does not allow to go beyond the array bounds:

void strncat_struct(char *buffer, size_t buffer_size, factor_t f)
{
    char tmp_buf[32];
    snprintf(tmp_buf, sizeof(tmp_buf), "%c, %d\n", f.c, f.k);
    strncat(buffer, tmp_buf, buffer_size);
}

int32_t main(int32_t argc, char **argv)
{
//...
    char buffer[256] = {0};
    for(i = 0; i < INIT; i++) {
        strncat_struct(buffer, sizeof(buffer), factors[i]);
    }
//...
}

Without using additional function. It is theoretically faster, couse there is no need to calculate string length:

int32_t main(int32_t argc, char **argv)
{
//...
    char buffer[256];
    char *buf_ptr = buffer;
    size_t buf_size = sizeof(buffer);
    for(i = 0; i < INIT; i++) {
        int32_t printed;
        printed = snprintf(buf_ptr, buf_size, "%c, %d\n", factors[i].c, factors[i].k);
        buf_ptr += printed;
        buf_size -= printed;
    }
//...
}
SergA
  • 1,097
  • 13
  • 21