6
I don’t know if I made myself clear in the title, but when using strcpy()
to copy a char*
to another when I put a format like this "teste"
it works normally, but when I put a string 3 letter format (digits in case), for example "2000"
it ends up merging this value for the destination with the next value the next time I use strcpy()
, follows the code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char nome[80];
char ano[4];
char diretor[80];
} Filme;
void analise(Filme *filme, const char *arg1, const char *arg2, const char *arg3)
{
Filme _filme = {
.nome = malloc(2),
.ano = malloc(2),
.diretor = malloc(2)
};
strcpy(_filme.nome, arg1);
strcpy(_filme.ano, arg2);
strcpy(_filme.diretor, arg3);
memcpy(filme, &_filme, sizeof _filme);
}
void carregar(Filme filmes[])
{
analise(&filmes[0], "E o Vento Levou", "1939", "Victor");
analise(&filmes[1], "teste", "998", "bar");
analise(&filmes[2], "Os Passaros", "1963", "Alfred Hitchcock");
}
int main()
{
Filme filmes[1000];
carregar(filmes);
printf("\nmain:\n");
printf("- Nome: %s\n", filmes[0].nome);
printf("- Ano: %s\n", filmes[0].ano);
printf("- Diretor: %s\n", filmes[0].diretor);
printf("----------------\n");
printf("- Nome: %s\n", filmes[1].nome);
printf("- Ano: %s\n", filmes[1].ano);
printf("- Diretor: %s\n", filmes[1].diretor);
printf("----------------\n");
printf("- Nome: %s\n", filmes[2].nome);
printf("- Ano: %s\n", filmes[2].ano);
printf("- Diretor: %s\n", filmes[2].diretor);
return 0;
}
Notice I ran this:
analise(&filmes[0], "E o Vento Levou", "1939", "Victor");
analise(&filmes[1], "teste", "998", "bar");
analise(&filmes[2], "Os Passaros", "1963", "Alfred Hitchcock");
When running the problem the output is this:
main:
- Nome: E o Vento Levou
- Ano: 1939Victor
- Diretor: Victor
----------------
- Nome: teste
- Ano: 998
- Diretor: bar
----------------
- Nome: Os Passaros
- Ano: 1963Alfred Hitchcock
- Diretor: Alfred Hitchcock
See that in "Gone with the Wind" and "Gone with the Birds" the years were merged with the director’s name, 1939Victor
and 1963Alfred Hitchcock
, already in the case of:
analise(&filmes[1], "teste", "998", "bar");
It has the correct output. I understand that I should make the year with int
, but I’m learning C and I’d like to better understand this part of the memory, I assume it was some typo of mine.
not just declare
char ano[5];
within the typeFilme
?– Marcelo Shiniti Uchimura
@Why Marcelouchimura? Is there reason, has explanation for this? And if the values are dynamic, how to ensure that do not pass these 4 digits, for example for some error the user type 99800, as I would?
– Guilherme Nascimento
I think what @Marcelouchimura was trying to say is that the terminator lacked the space, which Maneiro already focused on his answer.
– Isac
@Guilhermenascimento although more common in royal code, let the strings dynamic size becomes more difficult to manage. To learn I recommend not to use this at first. Anyway the year seems to me a clear case of being fixed size and should not be allocated out. The biggest reason to have a separate allocation is when you don’t know the size of the object, along with the reason when you don’t know the lifespan (of course when you know the size and it’s too big or the lifespan is longer than you can control locally).
– Maniero
@Isac was the missing explanation, and personally I think it’s nice for people to come here and say, "Do XYZ," but without explaining the motivations of this, it’s hard to determine why and learn. After all, we are not seeking to "just make a code work", we are seeking to truly learn. There’s a lot of code that works, but that doesn’t mean they’re right.
– Guilherme Nascimento
I agree with you 100% and that’s what I always try to do. Avoid promoting the blind copy of "things that work". So much so that there have been questions that I’ve answered with two or three code responses working, and I’ve only answered to explain the problem, so that the person can actually figure out where they went wrong and improve in the future
– Isac
@Guillhermenascimento, have you learned the reason for
[5]
now??– Marcelo Shiniti Uchimura
@Marcelouchimura I think it’s already pretty obvious that yes. And the problem of releasing comments "do XYZ" without explaining the reasons, already learned why this is not good for the community
– Guilherme Nascimento
@Guilhermenascimento, is not good for the community or is not good for those who have no interest (or time) to search and want to hand kissed?
– Marcelo Shiniti Uchimura
@Marcelouchimura has nothing to do with kissing, you’re not getting it, see my score, see how much I answer, see how I try to be judicious, I’m not asking for code ready ... this site is not a forum for technical support, I asked for guidance on how to work with strcpy, which is a totally valid question about the C string API, you just came in and made a vague, normal, and acceptable comment, but I asked why this is also fully valid, after all do Xyz without the least complicates explanation, even more for those who are studying.
– Guilherme Nascimento
@Guilhermenascimento, your score shows you have plenty of time to stay here.
– Marcelo Shiniti Uchimura
@Marcelouchimura a very prejudiced view without analysis and superficial to yours, Missing you look at the content of the posts and know how to receive a constructive criticism instead of getting personal and playing the victim, I made a fully valid inquiry into the purpose of the site, "Why 5?", simply could you have taken as something constructive and formulated a brief explanation, we’d be fine, but it’s clear that you’re trying to turn this into some kind of inquisition based on a trial of your own, totally superficial.
– Guilherme Nascimento
Let’s go continue this discussion in chat.
– Marcelo Shiniti Uchimura