Reputation: 333
I'm trying to append an array of integers into a string, basically I'm trying to get it into the following format: A :123456 where A is a character, given by a parameter, and 123456 is an integer array. I'm getting a bus error when running code, and I've done a search on here, and on google for the correct way to do something similar to what I have been hoping to achieve, but the examples don't directly relate to this so I was wondering if I could get some help. Thanks in advance
char *print_cards_played(Player *player) {
char *result = "";
int i = 0;
result += sprintf(result, "%c :", player->playerID);
while(player->cardsPlayed[i] != 0) {
result += sprintf(result, "%d", player->cardsPlayed[i]);
i++;
}
result += sprintf(result, "\n");
return result;
}
Upvotes: 1
Views: 1038
Reputation: 399793
You cannot "append" to a string defined by:
char *result = "";
All that gives you is a pointer named result
which points at a single read-only location in memory where the character '\0'
(the string terminator) is stored. You can't write to it.
You need to have an actual buffer of some suitable size:
char *result = malloc(128);
If that allocation succceeds, you have what is called a "heap buffer" which will stay around even if the function that did the allocation exits. You can safely return
this pointer, unlike a local array which would disappear as the function exited.
Then you can use sprintf()
, but of course not with +=
, you can't add strings in C. The best solution might be to sprintf()
to a temporary small string, then strcat()
that string onto the result
. You can keep track of the appended length to make it fast, but that's not critical for small problems on a modern machine.
Upvotes: 3
Reputation: 29126
You could write your own function and model it on the snprintf
paradigm, which takes a buffer and its length and fills that buffer with the desired string. If the buffer is too small, the string will be truncated, but still be a valid, null-terminated string:
int str_cards_played(char *buf, int nbuf, const Player *player)
{
int n;
int i = 0;
n = snprintf(buf, nbuf, "%c: ", player->playerID);
while(n < nbuf && player->cardsPlayed[i] != 0) {
n += snprintf(buf + n, nbuf - n, "%d", player->cardsPlayed[i]);
i++;
}
if (n < nbuf) n += snprintf(buf + n, nbuf - n, "\n");
return n;
}
You'd call the function like so:
char buf[20];
str_cards_played(buf, sizeof(buf), player);
printf("'%s'\n", buf);
If the allocation is on the stack like here, you don't have to worry about memory management.
Upvotes: 1
Reputation: 40145
try this
char *print_cards_played(Player *player) {
char result[128];// = "";
char *p = result;
int i = 0;
p += sprintf(p, "%c :", player->playerID);
while(player->cardsPlayed[i] != 0) {
p += sprintf(p, "%d", player->cardsPlayed[i]);
i++;
}
p += sprintf(p, "\n");
return strdup(result);
}
Upvotes: 2
Reputation: 8954
Use strcatInt
described below, to concat char* and int
char* itoa(int i, char b[]){
char const digit[] = "0123456789";
char* p = b;
if(i < 0){
*p++ = '-';
i *= -1;
}
int shifter = i;
do{ //Move to where representation ends
++p;
shifter = shifter/10;
} while(shifter);
*p = '\0';
do{ //Move back, inserting digits as u go
*--p = digit[i%10];
i = i/10;
} while(i);
return b;
}
char* strCatInt(char* str, int num) {
char numStr[10];
itoa(num, numStr);
return strcat(str, numStr);
}
Upvotes: 0
Reputation: 12658
Bus error is because of: char *p = "String Literal";
The reason stated clearly in c-faq as,
Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual, so the second declaration initializes p to point to the unnamed array's first element.
Upvotes: 0