hi forum
am trying to create a log as a big string and then send it to a file; so appending a small string but dont know how large it will eventually be, so doubling the size whilst the program is running.
the code below gives this:
Operands of '=' have incompatible types 'char
- ' and 'void *'.
how should I alter?
thanks in advance,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *resize_array(char *a, size_t new_size) {
char *save;
save = realloc(a, new_size);
if (save == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}
return save;
}
int main(void) {
char size=10;
char a[size];
int i = 0;
strcat("hello ", a);
/* Double the size of the array */
size *= 2;
a = realloc (a, size * sizeof (*a));
strcat("goodbye!", a);
printf("%s", a);
/* Destroy the array */
free(a);
return 0;
}
have since found this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void foo(char **string, char size)
{
*string = realloc(*string, size * sizeof **string);
if (*string == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}
}
int main(void)
{
char size = 6;
char *string1;
string1 = malloc(size);
sprintf(string1, "Hello");
size *=2;
foo(&string1, size);
strcat(string1, " World");
printf("%s", string1);
}
Quote from: bergin on November 07, 2009, 11:53:25 PM
hi forum
Operands of '=' have incompatible types 'char - ' and 'void *'.
how should I alter?
thanks in advance,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *resize_array(char *a, size_t new_size) {
char *save;
save = realloc(a, new_size);
if (save == NULL) {
fprintf(stderr, "Memory exhausted\n");
exit(EXIT_FAILURE);
}
return save;
}
int main(void) {
char size=10;
char a[size]; // you have seen, you need dynamic memory "char *a = (char*)malloc(strlen("hello") + 1);"
int i = 0;
strcat("hello ", a); // the content of a is undefined, so strcat is no good idea (sprintf as you use now is better) :)
/* Double the size of the array */
size *= 2; // I would calculate "size += strlen("goodbye!");"
a = realloc (a, size * sizeof (*a)); // you can not reallocate static memory
strcat("goodbye!", a);
printf("%s", a);
/* Destroy the array */
free(a);
return 0;
}
Yes Alex? I cant see your reply!
Quote from: bergin on November 16, 2009, 01:29:35 PM
Yes Alex? I cant see your reply!
I can't see it also at the first view. :(
But I have added some comments to your source code. I have changed my last answer so you perhaps can see it. ;)
Syntax:
char * strcat(char * restrict dst, const char * restrict src);
Quote from: timovjl on November 16, 2009, 09:18:33 PM
Syntax:
char * strcat(char * restrict dst, const char * restrict src);
I want not to say the function is undefined, i want to say this usage is no good idea.
strcat("hello ", a);
There are two problems:
1.) "hello" is a constant string and you are not able to add something.
2.) a is pointer to a memory, but the content of this memory is undefined. So if you have the strcat-function with the correct argument order (
strcat(a, "hello") ) you also don't know what you get (Pelles C (or the malloc-function from the OS) seems to initialize the content with zeros, but there also can be a long serie of f.e. 0x2D - a mistake in one of my programs which was open up when I changed from Windows NT to Windows 2000). The function
strcpy(a, "hello") or
sprintf(a, "hello") as
bergin used in his second example will be much better. ;)
I have found that writing directly to disk is much faster than concatenating a string and then writing that string to disk. The hard drive cache is very efficient. YMMV.
Robert Wishlaw