0

There is a char pointer variable, and its value coming from a function.

char* apple = ....(function call)

I wanted to print this as follows:

int len = strlen(apple);
for(i=0;i<len;i++){
    printf("%c ", apple[i]);
}

But in the console, it gives a question mark in a box as an output. What should I do, how should I print it? Thanks.

user438383
  • 5,716
  • 8
  • 28
  • 43
helloword
  • 5
  • 2
  • 1
    Welcome! Please show how you have initialised `cigar` and whatever it is pointing to, and where `len` comes from. Ideally post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), the shortest *complete* code that shows what you have tried. The best way to do that is by copy/paste, after you check it does exhibit the behaviour described. – Weather Vane Jul 10 '21 at 17:42
  • There still isn't enough information but I make a guess: please see [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c). A local variable ceases to exist when the function returns, so what is now being pointed to is indeterminate. – Weather Vane Jul 10 '21 at 17:51
  • Welcome to sof, you might get more answer when you show the calling fucntion or some simple example of it – cslrnr Jul 10 '21 at 18:07
  • @İlknur Baş - Why do you think the _question mark in a box_ were not correct? – Armali Jul 10 '21 at 18:14
  • 1
    FYI, the question-mark-in-a-diamond is what most systems print for characters which don't have a graphic representation, such as ASCII values below 32 or above 127, or unsupported Unicode characters. This might be the case if the function is returning a pointer to a buffer than contains non-ASCII (i.e., binary) data. – SGeorgiades Jul 10 '21 at 18:37
  • ```strlen``` requires that the ```char*``` ends with ```'\0'``` (null) character. Are you sure you initialized with ```'\0'``` at the end?. Also, Characters should be from the ASCII table. I mean they have to be English letters or some other limited number of characters. So, if you want to print 'ş' for example, it may print a ? in a box. I experience this when I try to print Arabic. – Mohamed Akram Jul 11 '21 at 16:53

2 Answers2

0

Your syntax seems legit. I highly suspect that cigar[i] donates the proper character that you are looking for. Trying affirming that by casting cigar[i] into a character using (char) cigar[i]. You might output cigar[i] as a string %s as a part of debugging where does it really point at.

Bilal Qandeel
  • 727
  • 3
  • 6
0

I dont see issue in the printing part, through the fucntion that retuns pointer to char array needs to be investigatd.

// In this example, getString function returns string literal
// That is being iterated in the next for loop over its length and prints its characters
#include <stdio.h>
#include <stdlib.h>

char *getString(void); // declare
int main() {
    char *apple = getString();
    int len = strlen(apple);
    for(int i = 0; i < len ; i++) {
       printf("%c ", apple[i]);
    }
    return 0;
}

char *getString() {
    return "somesthing";
}

Below example will print only printable ascii chars. From 0 to 31 , 0 is for null, 1 is for SOH and so on. Simply you cannot print control codes (ASCII codes < 32) if you print strange output is expected.

#include <stdio.h>
#include <string.h>

#define PRINTABLE_ASCII_CHAR_COUNT 96

/*
Printable chars list
"! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~"
*/

char *getASCIIs(void);
int main() {
    char *apple = getASCIIs();
    int len = strlen(apple);
    for(int i = 0; i < len ; i++) {
       // p << i << ((i % 16 == 15) ? '\n' : ' ');
       printf("%c ", apple[i]);
    }
    return 0;
}

char *getASCIIs() {
  static char buffer[PRINTABLE_ASCII_CHAR_COUNT];
  for (int i = 32, j=0 ; i <= PRINTABLE_ASCII_CHAR_COUNT; i++, j++) {
    buffer[j] = i;
  }
  return buffer;
}



enter code here
cslrnr
  • 694
  • 1
  • 8
  • 24