P
Paul Diaconescu
Hi,
I'm new to c and I don't have any clue to what I'm doing wrong. This
program gives me the output:
t1: 1234567890123456STRANGE
t2: STRANGE ^
(this is strange)
But only when the first string is 16 or 32 (...) characters long. I'm
using gcc 3.3 from apple (build 1495) on mac os x panther (gcc -pedantic
-ansi -Wall).
/ Paul
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct target {
char *t1;
char *t2;
};
void add_to_t (struct target *t, const char *s) {
char *temp;
static int target = 0;
temp = malloc(strlen(s) * sizeof(char));
strcpy(temp, s);
if (target == 0)
t->t1 = temp;
else
t->t2 = temp;
++target;
}
int main (void) {
struct target t;
char *s1 = "1234567890123456";
char *s2 = "STRANGE";
add_to_t(&t, s1);
add_to_t(&t, s2);
printf("t1: %s\nt2: %s\n", t.t1, t.t2);
return 0;
}
I'm new to c and I don't have any clue to what I'm doing wrong. This
program gives me the output:
t1: 1234567890123456STRANGE
t2: STRANGE ^
(this is strange)
But only when the first string is 16 or 32 (...) characters long. I'm
using gcc 3.3 from apple (build 1495) on mac os x panther (gcc -pedantic
-ansi -Wall).
/ Paul
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct target {
char *t1;
char *t2;
};
void add_to_t (struct target *t, const char *s) {
char *temp;
static int target = 0;
temp = malloc(strlen(s) * sizeof(char));
strcpy(temp, s);
if (target == 0)
t->t1 = temp;
else
t->t2 = temp;
++target;
}
int main (void) {
struct target t;
char *s1 = "1234567890123456";
char *s2 = "STRANGE";
add_to_t(&t, s1);
add_to_t(&t, s2);
printf("t1: %s\nt2: %s\n", t.t1, t.t2);
return 0;
}