Char pointer in printf() does not work

Asked

Viewed 497 times

1

I decided to make this code simple, where I have an empty vector and a function that fills it only with a letter that the user type, but I’m not able to print.

If I wear that printf() commented, it works, but if print out not at all. And if I try to print it in function main(), comes out with m 2 at the end. Why?

#include <stdio.h>
#include <string.h>
#define TAM 3

void preencher(char *str, char letra){
    int c;

    for (c=0; c<TAM; c++){
        *str = letra;
        //printf("%c", *str);
        *str++;
    }

    while (*str) printf("%c", str++);
}

int main(){
    char str[TAM], letra;

    scanf("%c", &letra);

    preencher(&str, letra);
}

3 answers

4


There are several errors in this code. You are passing a array by reference, of which array is already a reference.

Whether to use a variable to walk by array, use it instead of accessing the direct pointer. If you are going to access the pointer do not create a variable. But I would create the variable because walk by array without copying your address will cause the original note to change position and you will have later problems, which is the main problem you cannot use afterwards.

Isn’t putting the terminator that all string precise (I imagine you’re looking to create a string.

To print you don’t even need to do anything but call the print function as string. If you want to do it in your hand, it’s the same padding code, only it makes the impression.

That’s how it works:

#include <stdio.h>
#define TAM 3

void preencher(char *str, char letra) {
    for (int c = 0; c < TAM - 1; c++) str[c] = letra;
    str[TAM - 1] = 0;
    printf("%s", str);
}

int main(){
    char str[TAM], letra;
    scanf("%c", &letra);
    preencher(str, letra);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

There are better ways to do it.

2

@Maniero has already indicated to you the various mistakes you had, but I think it is important to mention that you should always be aware of what you are trying to do, because in many cases there are already functions ready for such.

In case you already include <string.h>, so you can use the function memset which allows you to assign a value to a byte memory zone. If you are assigning to a vector of char will work as expected.

Example:

#include <stdio.h>
#include <string.h>
#define TAM 3

int main(){
    char str[TAM + 1]; //+1 para o terminador
    char letra;
    scanf("%c", &letra);
    memset(str, letra, TAM); //colocar a letra repetidamente, igual ao preencher que tinha
    str[TAM] = '\0'; //colocar o terminador no fim da string
    printf("%s", str); //mostrar
}

Note that I used the size of string as TAM + 1 and put the terminator in TAM, effectively staying with TAM written letters. Alternatively you can allocate only TAM and put the terminator on TAM - 1 (how did @Maneiro), getting TAM - 1 written letters.

View the code in Ideone

0

If you want to print a vector of characters instead of a string, you can do the following::

#include <stdio.h>
#include <string.h>
#define TAM 3

void preencher(char *str,char letra){

int c;

printf("Conteudo do vetor: ");

 for (c=0; c < TAM; c++){
    str[c] = letra;
    printf("%c ", str[c]);
 }

}

int main(){

char str[TAM], letra;

printf("Digite uma letra:");
scanf("%c", &letra);

preencher(str,letra);
return 0;

}

Browser other questions tagged

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