5 Replies - 2208 Views - Last Post: 30 August 2019 - 04:13 AM Rate Topic: -----

#1 Caedmon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-August 19

I have a doubt about pointers to char in c.

Posted 25 August 2019 - 09:06 AM

#include <stdio.h>
int main(){
char *movie= "Iron Man";
printf("%d",*movie);
return 0;
}


When I run this code, I get an integer as an output. What does that number mean or represent? Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: I have a doubt about pointers to char in c.

#2 dr3am1nc0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 01-December 18

Re: I have a doubt about pointers to char in c.

Posted 25 August 2019 - 09:12 AM

View PostCaedmon, on 25 August 2019 - 09:06 AM, said:

#include <stdio.h>
int main(){
char *movie= "Iron Man";
printf("%d",*movie);
return 0;
}


When I run this code, I get an integer as an output. What does that number mean or represent? Thank you.


When you type printf("%d", varName) the "%d" indicates that you are going to print a number in integer format.

What you are looking to use is "%s" for string. Currently, what you are printing out is the ASCII equivalent of the character string in integer format.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2754
  • View blog
  • Posts: 4,411
  • Joined: 21-June 11

Re: I have a doubt about pointers to char in c.

Posted 25 August 2019 - 09:13 AM

A C string is a 0-terminated array of chars. So since your program contains the string "Iron Man", there's an array that contains the chars 'I', 'r', 'o', 'n', ' ', 'M', 'a', 'n' and '\0'. movie is a pointer to the first element of that array. Thus *movie is the first element of that array. In other words, *movie is equal to 'I'. And if you used "%c" as the specifier in your printf, the output would indeed be the letter I. But since you used "%d", it will print the numeric value of 'I', which is 73 in the ASCII encoding.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7482
  • View blog
  • Posts: 15,503
  • Joined: 16-October 07

Re: I have a doubt about pointers to char in c.

Posted 25 August 2019 - 03:22 PM

Perhaps the only real magic in C is c-strings, which do some interesting extras. But first, some code:
#include <stdio.h>
#include <stdlib.h>

#define T(fmt, x) printf("printf(\"%s\", %s) = ", fmt, #x); printf(fmt, x); printf("\n");

int main() {
    char *s = "Iron Man";

    T("%s", s);
    T("%c", *s);
    T("%d", *s);
    T("%c", s);
    T("%d", s);
    // what does the above mean?
    // you're actually getting a the pointer value
    // we can prove this
    T("%d", s + 1);
    T("%s", s + 1);

    return 0;
}



Result:
printf("%s", s) = Iron Man
printf("%c", *s) = I
printf("%d", *s) = 73
printf("%c", s) = 0
printf("%d", s) = 4210736
printf("%d", s + 1) = 4210737
printf("%s", s + 1) = ron Man



So, s is a pointer to your string data. When you do %s, you tell C you want to see the string data. However, C doesn't really know what the pointer type is and it will believe you when you tell its that it's something completely different. The printf("%d", s) strips away that string magic and you get the pointer value itself. Hopefully the code above demonstrates that a little.

This post has been edited by baavgai: 25 August 2019 - 03:22 PM
Reason for edit:: tag fail

Was This Post Helpful? 0
  • +
  • -

#5 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 766
  • Joined: 15-December 14

Re: I have a doubt about pointers to char in c.

Posted 29 August 2019 - 10:48 PM

char *s = "Iron Man";
printf("%s", s);



I have a question Baavgai why does the second example only show the first letter of the word Iron man?


I thought pointers referenced a certain part of the memory in a computer that the variable was stored. So shouldn't it show the entire word?

This post has been edited by albert003: 29 August 2019 - 10:49 PM

Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7482
  • View blog
  • Posts: 15,503
  • Joined: 16-October 07

Re: I have a doubt about pointers to char in c.

Posted 30 August 2019 - 04:13 AM

View Postalbert003, on 30 August 2019 - 12:48 AM, said:

char *s = "Iron Man";
printf("%s", s);



I have a question Baavgai why does the second example only show the first letter of the word Iron man?

Huh? No, to be clear, that one shows the whole thing. This one shows one letter:
printf("%c", *s) = I



The reason that shows one letter is a good question. It's a very C thing, actually. You have to understand that the C syntax for arrays is mostly just syntax sugar, so:
*s == *(s + 0) == s[0]
*(s + 1) == s[1]
*(s + 2) == s[2]
...



Thus, printf("%c", *s) is identical to printf("%c", s[0]). Make more sense?

The * against a pointer returns the value that address points to. If that pointer is an array, the value is the first value in the array. Note, in C, there is no intrinsic way to differentiate an pointer to an array from a pointer to value; it's just something you kind of write around.

This is what catches every student the first time they pass an array to a function. If an array is declared and allocated in scope, sizeof works. Once that array is passed, it "degrades" to a pointer and sizeof is only giving you the size of the pointer, regardless of the array syntax in the function sig.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1