1

I'm learning C language.

Here's my CODE

int main(void) { char * character = "abcd"; printf("%d \n", *character); int num = character; int * pnum = # printf("%s \n", * pnum); return 0; } 

I got result like : 97 and abcd .

I learned 97 is ascii code of 'a'.

and I want result 97 using pnum variable,

So i tried printf("%d", pnum) , printf("%d", *pnum) or somethings.

But I can't get 97 from pnum and num yet.

How can i get 97 using pnum or num?

2
  • 1
    int num = *character; int * pnum = # printf("%d \n", * pnum); Commented Jul 15, 2017 at 9:03
  • 2
    %s needs a pointer, so you've to use: printf("%s \n", pnum); and better, you use char * pnum; and a cast: *pnum = (char *) # Commented Jul 15, 2017 at 9:04

3 Answers 3

1

In general the program has undefined behavior . According to the C Standard(6.3.2.3 Pointers)

6 Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

For example sizeof( char * ) can be equal to 8 while sizeof( int ) can be equal to 4. That is an object of the type int can be unable to store a value of a pointer.

Instead of the type int in this declaration

int num = character; 

you should use type intptr_t declared in the header <stdint.h>

For example

#include <stdint.h> //... intptr_t num = ( intptr_t )character; 

So now the variable num contains the address of the first character of the string literal "abcd".

And after this declaration

intptr_t *pnum = &num; 

the pointer pnum has the address of the variable num.

Now to output the first character of the string literal you have at first to dereference the pointer pnum that to get the value stored in the variable num. This value is a representation of the address of the first character of the string literal. You need to cast it to the type char * and again to derefercen it.

Below is a demonstrative program that shows how it can be achieved. If you will not dereference the pointer then the whole string literal will be outputted.

#include <stdio.h> #include <stdint.h> int main(void) { char *character = "abcd"; printf( "%d\n", *character); intptr_t num = ( intptr_t )character; intptr_t *pnum = &num; printf( "%s\n", ( char * )*pnum ); printf( "%d\n", *( char * )*pnum ); return 0; } 

The program output is

97 abcd 97 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer
1

You can't get a result using int *pnum variable, because it is a pointer, and it is not pointing to an int value of 97.

In fact, there is no place in memory of your program where an int value of 97 is stored; there is a place where a char value of 97 is stored, but you cannot point an int pointer to it. Of course you can always cast pnum back to char* before dereferencing, but that is the same as your first printf:

printf("%d", *((char*)pnum)); 

If you want to get 97 into an integer variable, use num instead of pnum:

int num = *character; 

This converts char value of 'a' to its int code (97 on your system). Now you can point to it, or print the result directly:

int *pnum = &num; printf("%d %d\n", num, *pnum); 

1 Comment

Thanks for your answer
1

You could refer to: http://www.cplusplus.com/reference/cstdio/printf/

Now we know format specifiers:

  • d Signed decimal integer
  • i Signed decimal integer
  • u Unsigned decimal integer
  • o Unsigned octal
  • x Unsigned hexadecimal integer
  • X Unsigned hexadecimal integer (uppercase)
  • f Decimal floating point, lowercase
  • F Decimal floating point, uppercase
  • e Scientific notation (mantissa/exponent), lowercase
  • E Scientific notation (mantissa/exponent), uppercase
  • c Character
  • s String of characters
  • p Pointer address

So, Get the code:

int main(void) { char * chrs = "abcd"; char * pnum = chrs; int num = *chrs; printf("%c - %d - %d \n", *chrs, num, *pnum); return 0; } 

2 Comments

Why link a C++ reference when answering C question? Although this might work in the very particular case still C++ and C are different languages.
Thanks for your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.