Warning: assignment makes integer from pointer without a cast [-Wint-conversion] - c

Declaration of code:
$char Primeiro[5][20] = {"Pedro", "Tiago", "Ana", "Bruno", "Camila"};
$char Meio[5][20] = {"Oliveira", "Antunes", "Ferreira", "Santos", "Cunha"};
$char Sobrenome[5][20] = {"Cardoso", "Silva", "Azevedo", "Monteiro", "Soares"};
$char *vNomeCompleto[125][60];
$ vNomeCompleto[i][i] = strcat(Primeiro[iPrimeiro], strcat(Meio[iSegundo], Sobrenome[iTerceiro]));
I can not make the vector vNomeCompleto receive the other three vectors.
Can someone help me?

vNomeCompleto declared as array of char* pointers - no memory for strings are allocated, just pointers.
strcat(Meio[iSegundo], Sobrenome[iTerceiro]) - you trying to add 20 more characters to Meio[i] which is has 20 max len - expect "out of bound" error in some cases. The same for first strcat.

Here is a working example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i;
char Primeiro[5][20] = { "Pedro", "Tiago", "Ana", "Bruno", "Camila" };
char Meio[5][20] = { "Oliveira", "Antunes", "Ferreira", "Santos", "Cunha" };
char Sobrenome[5][20] = { "Cardoso", "Silva", "Azevedo", "Monteiro", "Soares" };
char vNomeCompleto[5][400];
for (i = 0; i < 5; ++i)
{
strcpy(vNomeCompleto[i], Primeiro[i]);
strcat(vNomeCompleto[i], Meio[i]);
strcat(vNomeCompleto[i], Sobrenome[i]);
}
return 0;
}
and the vNomeCompleto contains:
PedroOliveiraCardoso
TiagoAntunesSilva
AnaFerreiraAzevedo
BrunoSantosMonteiro
CamilaCunhaSoares

Related

C mutating struct properties by reference

I'm learning pointers in C and i came across a confusion between pointers X struts X functions
The goal: creating two structs and mutate properties inside them.
The path I'm going: I am creating these two structs and then passing its memory addresses to the mutate function, the function then prints and mutates some properties of these structs.
Result I get:
1: The name of the struct created is nod being entirely printed and its of the wrong struct passed, and the life property is not properly changed and printed to the screen.
2: On the terminal I get "Segmentation Fault", not sure why but I'm pretty sure its something wrong I did.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
void attackHero(Hero *hero, int *power) {
(*hero).life = (*hero).life - *power;
printf("Damage: %d\n", *power);
printf("Attacked hero: %s\n", (*hero).name);
printf("Hero's remaining life: %d\n", (*hero).life);
};
int main () {
Hero flash;
flash.power = 250;
flash.life = 500;
strcpy(flash.name, "The Flash");
Hero batman;
batman.power = 380;
batman.life = 700;
strcpy(batman.name, "Batman arkham knight");
attackHero(&flash, &batman.power);
return 0;
}
Result printed to the terminal (Vscode + gcc):
Here is the warning that I get when I compile your original code:
1.c:25:2: warning: ‘__builtin_memcpy’ writing 10 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
25 | strcpy(flash.name, "The Flash");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.c:30:2: warning: ‘__builtin_memcpy’ writing 21 bytes into a region of size 0 overflows the destination [-Wstringop-overflow=]
30 | strcpy(batman.name, "Batman arkham knight");
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you want to use the flexible array then you have to allocate space for it like this:
int main () {
Hero *flash = malloc(sizeof(*flash) + sizeof("The Flash"));
flash->power = 250;
flash->life = 500;
strcpy(flash->name, "The Flash");
Hero *batman = malloc(sizeof(*flash) + sizeof("Batman arkham knight"));
batman->power = 380;
batman->life = 700;
strcpy(batman->name, "Batman arkham knight");
attackHero(flash, &batman->power);
free(flash);
free(batman);
}
Here there the resulting code refactored a bit, and I added a error check for malloc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int power;
int life;
char name[];
} Hero;
Hero *createHero(int power, int life, const char *name) {
Hero *h = malloc(sizeof(*h) + strlen(name) + 1);
if(!h) {
printf("malloc failed\n");
exit(1);
}
h->power = power;
h->life = life;
strcpy(h->name, name);
return h;
}
void attackHero(Hero *hero, int power) {
hero->life -= power;
printf(
"Damage: %d\n"
"Attacked hero: %s\n"
"Hero's remaining life: %d\n",
power,
hero->name,
hero->life
);
};
int main(void) {
Hero *flash = createHero(250, 500, "The Flash");
Hero *batman = createHero(380, 700, "Batman arkham knight");
attackHero(flash, batman->power);
free(flash);
free(batman);
}
Alternatively use a fixed array (char [64] as suggested by #Diego) or a char * and allocate space to it. The former only needs 2 lines of code change from the original:
// largest name in use
#define NAME_LEN sizeof("Batman arkham knight")
typedef struct {
int power;
int life;
char name[NAME_LEN];
} Hero;
Whole lotta malloc() going on. Since the hero’s names are string literals (and assuming they don’t change), just change name[]; to const char *name in the structure and initialize via simple assignment:
flash.name = "The Flash";
batman.name = "Batman arkham knight";
No worries about malloc() failures, name sizes or free() requirements.

How do i put a word into an array

so this is part of a kind of menu, the only problemis that the word is not getting into the array "frase" i have already tried with frase [ ] = "the word" but idk why it wont work
if(lvl==1)
{
printf("lvl 1\n");
if (opc==1)
{
printf("Animales\n");
a = rand() %3 + 1;
printf("%d", a);
if (a=1)
frase <= "pato";
if (a=2)
frase <="ganso";
if (a=3)
frase <= "avispa";
}
if (opc==2)
{
printf("comida\n");
a = rand() %3 + 1;
if (a=1)
frase <="pasta";
if (a=2)
frase <="pizza";
if (a=3)
frase <="pastel";
}
if (opc==3)
{
printf("paises\n");
a = rand() %3 + 1;
if (a=1)
frase <="peru";
if (a=2)
frase <="brasil";
if (a=3)
frase <="egipto";
}
}
`
I suggest you solve this by modeling your data. In this case with a array of structs. Then you index into to obtain the relevant data:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main() {
struct {
const char *opc;
const char **frase;
} data[] = {
{"Animales", (const char *[]) { "pato", "ganso", "avispa" }},
{"comida", (const char *[]) { "pasta", "pizza", "pastel" }},
{"paises", (const char *[]) { "peru", "brasil", "egipto" }}
};
srand(time(0));
int opc = rand() % 3;
printf("lvl 1 %s %s\n", data[opc].opc, data[opc].frase[rand() % 3]);
return 0;
}
If you have a lot of data put the data in a file and write a function to build the struct at start-up. A special case of this approach is to store the data in a lightweight database like SQLite, then you can query for the relevant data at run-time or load it all it upon start-up.
You many no longer need to copy the frase, but if you want to use a strcpy:
char frase[100];
strcpy(frase, data[opc].frase[rand() % 3]);
Multiple things to be improved in the code. The if(a=1) should be changed to ==. Not sure what you mean by frase<="pato", strcpy or strncpy should be used. Please refer the following sample code.
void copytoarray(char *array, char *word, unsigned int len)
{
if(array == NULL || word == NULL)
{
return;
}
strncpy(array, word, len);
}
int main(void) {
char frase[15] = {'\0'};
int a, lvl =1;
int opc =1;
if(lvl==1)
{
printf("lvl 1\n");
if (opc==1)
{
printf("Animales\n");
a = rand() %3 + 1;
printf("%d\n", a);
if (a==1)
copytoarray(frase, "pato", strlen("pato"));
if (a==2)
copytoarray(frase, "ganso", strlen("ganso"));
if (a==3)
copytoarray(frase, "avispa", strlen("avispa"));
}
}
printf("Word: %s\n ",frase);
}

Getting error: expected expression before ‘{’ token in C while trying to verify struct

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LEN_ID 3
#define LEN_P 30
#define LEN_CIDADE 50
#define AT 40
typedef struct aeroporto
{
char id[LEN_ID + 1];
char pais[LEN_P + 1];
char cidade[LEN_CIDADE + 1];
} Aeroporto;
int findavailablespot(Aeroporto l[AT])
{
int i = found = 0;
for (;i<AT;i++) {
if (l[i] = {"aaa","bbb","ccc"}) //Error in this line
break;
if (found)
return i;
else
return -1;
}
}
So i am creating the structure aeroporto then a vector made up of aeroportos and i want to check if {"aaa","bbb","ccc"} shows up inside the vector.
Help?
Sorry for the formatting, new at this
You have to use strcmp() to compare strings. There's no shortcut for doing this with all the members of a structure, you have to test each one individually and combine with &&.
You also forgot to set found before breaking out of the loop.
int i = 0, found = 0;
for (;i<AT;i++) {
if (strcmp(l[i].id, "aaa") == 0 && strcmp(l[i].pais, "bbb") == 0 && strcmp(l[i].cidade, "ccc")) {
found = 1;
break;
}
}

Function in C only working when an unrelated line is present

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct card_t{
char value;
char suit[50];
} card_t;
card_t draw(){
card_t karta;
int v = (rand() % 13)+2;
int s = (rand() % 4)+1;
if(v<=9){
karta.value = v +'0';
}else{
if (v==10)
karta.value='T';
if (v==11)
karta.value='J';
if (v==12)
karta.value='Q';
if (v==13)
karta.value='K';
if (v==14)
karta.value='A';
}
if (s==1)
strcpy(karta.suit, "of Spades");
if (s==2)
strcpy(karta.suit, "of Hearts");
if (s==3)
strcpy(karta.suit, "of Diamonds");
if (s==4)
strcpy(karta.suit, "of Clubs");
return karta;
}
void face_up(card_t deck[],int size){
for(int i=0;i<=size;i++){
printf("%c %s\n",deck[i].value ,deck[i].suit);
}
}
int main()
{
int size;
card_t *deck;
deck = malloc(100*sizeof(char));
card_t karta;
karta=draw();
for (int i=0; i<100 ; i++){
deck[i]=karta;
if(strcmp(deck[i].suit,"of Spades")==0 && deck[i].value=='A'){
size=i;
break;
}
karta=draw();
/*THIS ONE*/printf("%c %s\n",deck[i].value ,deck[i].suit);
}
face_up(deck,size);
free(deck);
return 0;
}
If I remove the line marked with /THIS ONE/ , the function face_up won't print anything , but if the line is there it works. Any ideas ? Tried it several times and its the same thing .
I would get double print if i leave it there , which i dont need.
Im sorry for the bad code / formatting but i am kinda new to this and yeah ...
Thank you for your help in advance.
This line here deck = malloc(100*sizeof(char)); allocates memory on the heap to store an array of 100 chars. What you want to do, is to allocate an array of 100 card_t. To do this just replace the statement with the following one:
deck = malloc(100*sizeof(card_t));

Strange Behavior with String Arrays in C

I've been having some difficulties with strings in C - especially when it comes to File I/O. I've looked through some previous threads to see how to make string arrays in C, and I have come up with this.
void CreateBiomes()
{
const int STRING_LENGTH = 32;
const int BIOME_COUNT = 63;
const char *biomes[BIOME_COUNT][STRING_LENGTH+1] = {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills", "void"};
for(int i = 0; i <= BIOME_COUNT; i++)
{
printf("%s\n", *biomes[i]);
}
return;
}
The issue is - this code only works for "beaches" and "mutated_mesa" before the program crashes. Everything compiles great, it just won't process any of the other strings I've listed in my array, instead, it prints a (null). Why is this?
Try replacing the declaration
const char *biomes[BIOME_COUNT][STRING_LENGTH+1]
as follows.
const char biomes[BIOME_COUNT][STRING_LENGTH+1]
Your array was a 2-D array of pointers, and, you indexed one too many in the printf loop.
I have changed this to a 1-D array of pointers, and also removed the hard coded sizes. Instead I replaced the last string "void" with a NULL pointer, and used that to control the loop.
#include <stdio.h>
void CreateBiomes(void)
{
const char *biomes[] = {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills",
NULL };
for(int i = 0; biomes[i] != NULL; i++) { // changed loop control
printf("%s\n", biomes[i]); // changed argument passed
}
}
int main(void){
CreateBiomes();
return 0;
}
Please note that this function won't do much good, because biomes, a local variable, will not be accessible after the function returns.
Try this way, your code should work.
void CreateBiomes()
{
int BIOME_COUNT = 63;
char *biomes[]= {"beaches", "birch_forest", "birch_forest_hills", "cold_beach", "deep_ocean", "desert", "desert_hills", "extreme_hills", "extreme_hills_with_trees", "forest", "forest_hills", "frozen_ocean", "frozen_river", "hell", "ice_flats", "ice_mountains", "jungle", "jungle_edge", "jungle_hills", "mesa", "mesa_clear_rock", "mesa_rock", "mushroom_island", "mushroom_island_shore", "mutated_birch_forest", "mutated_birch_forest_hills", "mutated_desert", "mutated_extreme_hills", "mutated_extreme_hills_with_trees", "mutated_forest", "mutated_ice_flats", "mutated_jungle", "mutated_jungle_edge", "mutated_mesa", "mutated_mesa_clear_rock", "mutated_mesa_rock", "mutated_plains", "mutated_redwood_taiga", "mutated_redwood_taiga_hills", "mutated_roofed_forest", "mutated_savanna", "mutated_savanna_rock", "mutated_swampland", "mutated_taiga", "mutated_taiga_cold", "ocean", "plains", "redwood_taiga", "redwood_taiga_hills", "river", "roofed_forest", "savanna", "savanna_rock", "sky", "smaller_extreme_hills", "stone_beach", "swampland", "taiga", "taiga_cold", "taiga_cold_hills", "taiga_hills", "void"};
int i;
for(i = 0; i < BIOME_COUNT-1; i++)
{
printf("%s\n", biomes[i]);
}
return;
}

Resources