1

I'm new to C and currently learning about Pointers. All I know is a pointer is basically a variable that contains the address of another variable and to get its data we need dereference pointer :

int i = 10;
int *pi = &i;
printf("%d",*pi);

But watching some tutorials on yt I also see

char *p = "Hello";
printf("%s",p); //print out Hello//

I also test it myself

int *i = 10;
printf("%d",i); //print out 10//

I have searched about it and know it's a kind of read-only memory instead of char []. But p and i here are pointers and how can they work with %s and %d directly but not dereferencing *i *p. Can anyone explain it to me pls.

Nicholas
  • 13
  • 2
  • 1
    A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) (ignore the titles, the answers discuss pointer basics) The difference in your case is `%d` expects an integer as the argument while `%s` expects a pointer `:)` `int *i = 10;` assigns the address `10` as the value for pointer `i`. It is likely outside any valid memory range for your program. – David C. Rankin May 22 '21 at 02:26
  • 2
    10 is basically treated as a pointer. And then it's again treated as an `int`. Both of these conversions are wrong. ALWAYS USE YOUR COMPILER'S WARNINGS. I use `-Wall -Wextra -pendantic` with `gcc`/`clang`. The code is basically garbage and just happens to give the "expected" result. – ikegami May 22 '21 at 02:27
  • `int *i = 10;` If you did not get a warning on this line then turn your compiler warning level up. – dxiv May 22 '21 at 02:46
  • It's fine to ask questions -- and welcome to stackoverflow :) I would advise you to pick up a good introductory book on C and read it. The basics of strings and pointers will be explained, and (in my opinion) it's pretty hard to learn C correctly by making your own way and relying on internet tutorials since C is a deceptively tricky language. Here's a good list of books: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – Paul Hankin May 22 '21 at 07:25

4 Answers4

0

A pointer doesn't necessarily contain address of another variable, it contains an address of some data, which can but doesn't need to be in a variable. For example, the pointer may point to dynamically allocated data, or to data that is part of an array, or to static data that ships with the executable.

In case of char *p = "hello", p points to the first character of a character array prepared by the compiler, and which contains the letters hello followed by a terminating zero character. The %s specifier is designed to receive such a pointer and print the letters up to the zero character, which is why it works.

Your second example, where you print a pointer using %d is quite different in nature. It abuses the fact that addresses have integer representations, and that T *x = <some integer> implicitly casts the integer to the corresponding memory address. In your case you create an invalid pointer pointing to the invalid address 10. Then you proceed to call printf with the %d specifier without dereferencing the pointer. Since %d expects an integer, and the address is also internally represented as an integer, this prints the invalid address 10 the pointer points to. However, pointers and integers don't have compatible memory representations, so this kind of code only works by accident, and exhibits undefined behavior.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
0

To generalize the answers already given, a pointer is simply a variable that contains a memory address. How the code attempts to use that memory address depends on the context you give to it. A pointer can point to data, or even code, in the form of a function pointer. You could even assign any arbitrary value you want to a pointer, and the code will attempt to use it, though that likely will fail or give unpredictable results.

It's perfectly legal to do this:

int *i = (int *)0x12345678;

and dereferencing that pointer (int x = *i;) is also perfectly legal, but will almost certainly fail, because 0x12345678 is likely not an address that is valid when your code runs. Even if it is, the code generated by the C compiler will attempt to access those bytes in a manner that may not correspond to whatever is stored there. In the above case, if you're on, say, a 32-bit x86, the code will interpret the 4 bytes starting at 0x12345678 as a 32-bit signed integer in little-endian format. Whatever is actually there may not result in a usable value.

sj95126
  • 6,520
  • 2
  • 15
  • 34
-1

hi Have Following Have Example Of Pointer With Array

enter code here

#include<stdio.h>
#include<conio.h>
void main()
{
    int x,*px,i,**pxx;
    x=10;
    px=&x; //Right Way To iniFor Pointer
    pxx=&px+1; //store address of another pointer
    clrscr();//we use px+1 because have 2 address 1 is x and 2 px

    printf("\n\n passed value of x in pointer");
    printf("\n\n the value of x:-%d",x);
    printf("\n the value of px:-%d",*px);//Using * u access a value
    printf("\n the value of pxx:-%d",**pxx);

    printf("\n\n the address of pointer variable");
    printf("\n\n the &x address:-%p",&x);
    printf("\n the px &address:-%p",&px); //using %p Or %x Acces address
    printf("\n The address of pxx:%p",&pxx);
    for(i=0;i<5;i++)
    {
        px=px+1;
        *px=i;
        printf("\n%d %p",*px,px);
    }
    printf ("\n\nThe array of Pointer\n");
    for(i=0;i<5;i++)
    {
        pxx=pxx+1;
        printf("\n%d %p",*pxx,pxx);
    }

    getch();
}

in this example pointer used as a array **pxx is have 2 address 1 is x 2 is pxx that reason we used pxx=&px+1 to access address of px

-2
*-represents value at certain variable.
&-represents address of certain variable.
*p=10 let's say the address of p might be "662543" so now by you have stored the value 10 in the address "662543".
and nor if you print 
*p - output will be 10, and
&p- output will be 662543.
and talking about the % sign its a format specifier. like %d for integer, %f for float and %s for string etc.

and difference between %d and %i is as follows-

%d takes integer value as signed decimal integer i.e. it takes negative values along with positive values but values should be in decimal otherwise it will print garbage value.( Note: if input is in octal format like:012 then %d will ignore 0 and take input as 12) Consider a following example. 
 
%i takes integer value as integer value with decimal, hexadecimal or octal type. 
To enter a value in hexadecimal format – value should be provided by preceding “0x” and value in octal format – value should be provided by preceding “0”. 
sam2611
  • 455
  • 5
  • 15