user3332897
user3332897

Reputation: 33

Trying to do something with pointers

Hello I am new with pointers and I'm trying to make with them program but it does not work.

The program - will get a number and with the Pointer grow it in one.

My code-

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int x = 0;
    int* px = NULL;

    printf("Enter a number: \n");
    scanf("%d",x);

    *px = x;
    px++;
    x = *px;

    printf("%d",x);

}

Unfortunately the program does not work If you can help me I would love.

Upvotes: 0

Views: 126

Answers (4)

Unavailable
Unavailable

Reputation: 681

Here's what should be fixed in your code with detailed explanations:

px = &x; // You need to assign a memory address to your pointer by using & address of operator.

Always keep in mind that you should never use an unassigned pointer in your program without any intentional purpose. Pointers are data types that can store memory addresses (not exactly the physical address, but virtual address) of some other data structures or subroutine entry points.

scanf("%d", &x); // You need to pass the address of your variable as an argument.

scanf function scans input from stdin file descriptor, in this case it accepts data input from your keyboard device where stdin file is redirected to by default. Prototype of the function is declared in stdio.h header file as the following:

int scanf(const char *format, ...);

Therefore, you need to pass a pointer or address of a declared variable as an argument.

*px = x; // You can assign a value to your variable which is referenced by a pointer by using * dereference operator.

However, in your code this statement is meaningless. Operation is equal to statement below:

  x = x;

Now, let's take a look at the statements below. They're both doing the same thing...

  x = 10;   // Value is assigned directly.

*px = 10;   // Value is assigned via pointer by using dereference operator.

If you would like to gain solid understanding on pointers, you may find character arrays and string operations more didactic and interesting.

Upvotes: 0

ajay
ajay

Reputation: 9680

I guess you are trying to increment x using a pointer to it, without accessing it directly. You program as such would crash due to segfault because you are dereferencing a null pointer. I suggest the following changes -

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int x = 0;
    int *px = NULL;  // initialize to NULL

    printf("Enter a number: \n");

    // scanf takes a pointer to the variable where 
    // it writes the value read from stdin

    scanf("%d", &x); 

    px = &x;  //  make px point to x
    (*px)++;  // increment the object pointed to by px

    printf("%d", x);

}

Note that *px++ without the parentheses would post-increment the pointer itself and dereference it because ++ has precedence than *. Therefore it's the same as *(p++). However, you want to increment the object pointed to by px, not px itself. Therefore you must do (*px)++.

Upvotes: 0

Oliver
Oliver

Reputation: 824

It would be of great help if you would elaborate on "the program does not work" - Programs can "not work" in many ways. By looking at your code, I see these points:

  1. Wrong scanf use:

    scanf("%d", x);
    

    scanf takes pointers to variables for each argument so it knows where to write to (which memory location), so the correct call would be:

    scanf("%d", &x);
    
  2. Write to null pointer and address incrementation instead of value incrementation

    *px = x;
    

    You initialized px to point to NULL at the beginning of main. *px dereferences the pointer so you can write the actual memory the pointer points to. I guess what you're trying to do here is:

    • Assign the address of x to px
    • Increment x by using the indirect reference of x in px
    • The reassign x to itself by de-referencing px and assigning it to x again

    To take the address of a variable, you have to use the prefix & operator and assign it directly to the pointer:

    px = &x;
    

    To increment the value the pointer points to, you have to first de-reference it and then operate on it with the increment operator:

    (*px)++;
    

    Your self-assigned of x to x through *px is correct, though ;)

Upvotes: 3

Pandrei
Pandrei

Reputation: 4951

Here is what you are doing

scanf("%d",x); //read the number

*px = x; //assign the value of the pointer, the value of the number
px++;    //increment the pointer
x = *px; //assign x the value of the pointer

printf("%d",x);//display the number

So you are assigning the value of the pointer the value of x, than incrementing the pointer and assigning the value of the pointer back to X. But by now the pointer is points to a different memory address and the value at that address must be something on the stack...

Here is what you should be doing:

scanf("%d",x); //read the number

px = &x; //assign the value of the pointer, the value value of x
(*px)++;   // the same as x++ since px holds the address of x

printf("%d",x);//display the number

It's overcomplicating things but this is how you would do it using a pointer

Upvotes: 0

Related Questions