C printing specific char depending on input from string with pointers

pointer to string c
array of pointers to strings in c example
character pointers and functions in c
how to pass string to a function in c using pointers
read string using pointer in c
how to store a string in char pointer in c
how to create pointer to string in c
pointers in c

Given this:

 char c[]={ '0', 0, 'o', 'a', 'i', 'e' };

 char s_format[] = { "\nBra"
   "v%c!\n" };

The object of this program is to print different lines of code depending on the input, which can be:

 ./nameofmyfile             Bravo
 ./nameofmyfile mp          Bravi
 ./nameofmyfile f           Brava
 ./nameofmyfile fp          Brave

In my main I have two char pointers:

char *q = c;      //this one is initialized at position c[0]
char *p = NULL; 

I suppose I could use only the *q pointer in order to print either the character o, i, a or e in the s_format without using a switch construct.

Would this for loop be perhaps correct?

for (unsigned int i=2; i<sizeof(c); ++i) {
     printf("%c", *(q+i), s_format[4]);

Or should I perhaps use a series of if's?

if ( argc > 3 && strlen(argv[3]) ) {
    if ( *(argv[3]) == 'f' )
        i = 3;

if ( argc > 4 && strlen(argv[4]) ) {
    if ( *(argv[4]) == 'mp' )
        i = 4;

if ( argc > 1 && strlen(argv[5]) ) {
    if ( *(argv[5]) == 'fp' )
        i = 5;

Use this:

#include <stdio.h>
#include <math.h>

char c[]={ '0', 0, 'o', 'a', 'i', 'e' };

const char * s_begin = "Brav";
const char * s_end ="!\n";

int main()
    int i = 3;
    printf("%s%c%s",  s_begin,c[i],s_end); //prints brava
    return 0;

if you accept my proposition, you can upvote it by clicking on the upper arrow and you can also accept the answer by pressing the V

C Program to Print String using Pointer, write a C program to print a String character by character using a pointer variable. a char array to hold the input string and we have declared a char pointer. In the following program we have declared a char array to hold the input string and we have declared a char pointer. We have assigned the array base address (address of the first element of the array) to the pointer and then we have displayed the every element of the char array by incrementing the pointer in the while loop .

I think maybe you want something like

int *q = c;
if (argc == 2) {
    if (argv[1][0] == 'f') {
        if (argv[1][1] == 'p') { //assume argv[1][2] == 0
            q += 5;
        } else {
            q += 3; //assume argv[1][1] == 0
    } else { //assume argv[1][0] == 'm'; argv[1][1] == 'p'; argv[1][2] = 0
        q += 4;
} else { //assume argc == 1
    q += 2;
printf(s_format, *q);

Strings as arrays, as pointers, and string.h, A character array can have more characters than the abstract string held in it, as printf("Third char is: %c\n", label[2]);. which prints out the third character, n. The ordering for strings is lexical order based on the ASCII value of characters. The reason why the first case (with "%c") only printed the first character is that %c means a byte and *p means the (first) value which p is pointing at. %s would print the entire string. char *p = "abc"; printf(p); /* If p is untrusted, bad things will happen, otherwise the string p is written. */ printf("%c", *p); /* print the first byte in the string p */ printf("%s", p); /* print the string p */

Posting here my answer for the Others:

if(argc>1 && strlen(argv[1])>1 && argv[1][1]==p[0])
printf(s_format, q[i]);

Search Within A String - How to play with strings in C, The strchr function returns the first occurrence of a character within a string. The strrchr a string. They return a character pointer to the character found, or NULL pointer if the character is not found. printf("Length of string: %d\n", strlen(str));. Write a C program to input elements in an array and print array using pointers. How to input and print array using pointer in C programming. Learn C programming, Data Structures tutorials, exercises, examples, programs, hacks, tips and tricks online.

Character Array and Character Pointer in C, The type of both the variables is a pointer to char or (char*) , so you can pass whose formal argument accepts an array of characters or a character pointer. We can assign a new string to arr by using gets() , scanf() , strcpy() or by all whitespace in the input buffer */ scanf(" %c", ptr+i); } printf("\nPrinting� Do I have to cast the pointers with (void *) in order to print them? And when running I get something like this: $ ./main-bin The value of s is: 0xbfb7c860 The direction of s is: 0xbfb7c860 The value of p is: 0xbfb7c860 The direction of p is: 0xbfb7c85c The direction of s[0] is: 0xbfb7c860 The direction of s[1] is: 0xbfb7c861 The direction of s

Printf: Reading User Values, Note that scanf uses the same sort of format string as printf (type man scanf for more info). address of the variable (this will not make sense until we discuss pointers). You must use the & operator in scanf on any variable of type char, int, or float, have the same types in the same order as those specified by the operators. The type of both the variables is a pointer to char or (char*), so you can pass either of them to a function whose formal argument accepts an array of characters or a character pointer. Here are the differences: arr is an array of 12 characters. When compiler sees the statement:

C program to print a string character by character using pointer , Here, we have two variables, str is a string variable and ptr is a character pointer, that will point to the string variable str. First of all, we are reading string in str and � String Input: Read a String ; String Output: Print/Display a String ; fputs() function ; puts function ; The string library ; Converting a String to a Number ; Declare and initialize a String. A string is a simple array with char as a data type. 'C' language does not directly support string as a data type. Hence, to display a string in 'C', you

  • You have one extra parameter in printf("%c", *(q+i), s_format[4]). Overall it is not clear what you are trying to ask.
  • Your loop has absolutely nothing to do with the problem. It would rather seem that you want to overwrite s_format[4] with another character. And why do you store a line feed at index 0 for?
  • Is it perhaps the *(q+i) one? Is there any other (string related or not) printing function I could use to print the value of the pointer q as it increments with i all within the array s_format? Or perhaps I could use this line of code in the for loop: s_format[4]=*(q+i); printf("%c", s_format); ?
  • I apologize for the unclear question; unfortunately I have no idea how to get that output and the loop was my best guess (first program in c ever). How could I get those outputs, and do I have to use the s_format string?
  • Thank you for your answer!
  • But I've just noticed that the output needs to be "Bravo" with any input, even bad ones (like ./nameofmyfile 429tfyh), and unfortunately this way still gives me the output "Bravo". Any tips on how to achieve that?
  • When I say "assume" make the code be sure about that before updating q.