Dynamic Char Allocation with pointers

Asked

Viewed 85 times

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.

1 answer

2


In the first program you wrote two loops, the first you tried to draw the track, 70 characters long, which is equivalent to the loop below (I took the liberty of replacing the quotation marks with apostrophes):

for (int i = 1; i < 70; i++)
{
    ponteiroLebre[i] = '.';
    ++ponteiroLebre;

}

What is happening here is that the pointer moves forward one position, and is increased by i, in other words, one point is being written in one position, the other is not. Writing data into memory goes beyond what is allocated (probably up to position 139) and the operating system does not let this happen by closing the program. To fix this, simply delete the line where the pointer is incremented:

for (int i = 1; i < 70; i++)
{
    ponteiroLebre[i] = '.';

}

In the second loop, the value in ponteiroLebre[1] is written ten times, with one space in front and one behind, that is, two spaces between the characters represented by the character contained in ponteiroLebre[1].

In the second program, you write only one loop, it will write the clue in memory, but you have to replace the quotation marks with the apostrophes. It’s not clear enough what you want to do, but I offer you this little program that runs the parts that work in your program, it may help you to think more about how to write it.

#include <stdio.h>

#define COMPRIMENTO_DA_PISTA 70
int main(int argc, char const *argv[])
{
        char pista[COMPRIMENTO_DA_PISTA];

        for (size_t i = 0; i < COMPRIMENTO_DA_PISTA; i++)
                pista[i] = '.';
        pista[COMPRIMENTO_DA_PISTA - 1] = '\0';
        for (size_t i = 1; i < COMPRIMENTO_DA_PISTA; i++) {
                pista[i - 1] = 'L';
                printf("%s\n", pista);
                pista[i - 1] = '.';
        }
        putchar('\n');
        return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.