0
I am a beginner in C and I have a question that I am not able to solve the way I would like and I wanted to know why this.
Contextualizing, I have to make a run that changes the positions in an array, exemplifying: 1° L... 2° .L.. 3° . .L. 4° ... L
My idea was that the array initialize with run[3] = "L" and the rest of the array be filled with the dots automatically with a loop and for that I used a pointer, but it is not working as I expected and always gives this error: "Warning: assignment makes integer from Pointer without a cast"
The code I’m trying to make is this:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char const *argv[])
{
int corridaTart = 0, corridaLebre = 0;
char posicaoTart[70] = "L";
char posicaoLebre[70] = "T";
char *ponteiroTart = posicaoTart;
char *ponteiroLebre = posicaoLebre;
int randNumeroTart;
int randNumeroLebr;
srand(time(NULL));
printf("%c", *ponteiroLebre);
for (int i = 1; i < 70; i++)
{
*(ponteiroLebre+i) = ".";
++ponteiroLebre;
}
for (int i = 0; i < 10; i++)
{
printf(" %c ", *(ponteiroLebre+1));
}
return 0;
}
Another one I tried to do with struct was like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, char const *argv[])
{
struct corrida
{
int corrida;
char posicao[70];
int rand;
};
struct corrida lebre, *ponteiro;
struct corrida tart, *ponteiro2;
srand(time(NULL));
ponteiro = &lebre;
ponteiro2 = &tart;
ponteiro->posicao[0] = "L";
ponteiro2->posicao[0] = "T";
for (int i = 0; i < 70; i++)
{
ponteiro->posicao[i] = ".";
printf("%c",lebre.posicao[i]);
}
return 0;
}
Do not confuse double quotes ("), delimiter of a string, with single quotes ('), delimiter of a character. To assign a character you use the operator
=
but for string you must use the strcpy function of <string. h>. There is not much sense in this loop where you take the content of an address plus the value of the loop control variable and soon after increments the address, you will always be skipping positions of your array.– anonimo