Iterate an array of characters in c


I want to print each element of the array but it generates a segmentation fault

This is the code:

#include <stdio.h>
int main(){
char arreglo[3]={'c','f','d'};
  char *puntero;
  for(int i=0;i<3;i++){
      printf("El valor de puntero es %s \n",*puntero);


If in printf instead of putting *puntero I only put puntero os so

printf("El valor de puntero es %s \n",puntero);

Print me like this:


I do not know what's wrong.

asked by Karina 14.06.2018 в 23:11

1 answer


To iterate an array of characters in c, you can do it by iterating the array and using %c to print characters in C.

int main() {
   char arreglo[3]={'c','f','d'};
  //char *puntero;
  for(int i=0;i<3;i++){
      printf("El valor del elemento: %d en el arreglo es: %c \n", i , arreglo[i]);

to have as output:

El valor del elemento: 0 en el arreglo es: c 
El valor del elemento: 1 en el arreglo es: f 
El valor del elemento: 2 en el arreglo es: d 

For more information about how you specify the format to print, you can see this excellent response from @NaCl :

What is the use of the operator% in printf of variables in C language?

answered by 15.06.2018 в 01:27