POINTERS

Hello again everyone! I am confused by the instructions given. Can someone help and teach how to do it? I have no experience in programming I hope you can help me. Thank you!

EDIT: It has to be in C language

TASK:
1.Given three integer variables, x= 512, y=1024,and z=2048, write a program to print out their left values as well as their right values.

2.Write a program to update the value of the double variable flt_num from 123.45 to 543.21 by using a double pointer.

3.Given character variable ch and ch= ‘A’ , write a program to update the value of ch to decimal 66 by using a pointer.

4.Given that x = 5 and y = 6, write a program to calculate the multiplication of the two integers and print out the result, which is saved in x,all in the way of indirection (that is using pointers)

Last edited on
1) this makes no sense. you need to ask your professor or review what was said in class.
I mean, the right of x = 512 is x, so cout << x and the left is 512 so cout << "512" but that is really silly to ask for and a weird way to say it.

2) double fit_num{123.45}; //make the variable
double * pfn = & fit_num; //get a pointer to it
*pfn = 543.21; //now cout fit_num, it is changed because you modified it from the pointer.

3) more or less the same as above, with a little twist. can you do it?
4) more of the same, get past 2 and this is just a little more.

Last edited on
How do I know I'm doing it right? What does it look like when you run it?
You have to add some extra print statements to prove to yourself that it is working.
I still can't get it right, It should be in C language
post what you have that isn't right.
have you already studied arrays?
Last edited on
Not yet :/ our professor didnt provide any notes and i dont have an idea how to write the program. Our professor didnt upload the file so i dont have any guides at all. Also the examples on the internet are not helping me
UP
my C is a little rust, but maybe this will get you started:

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main()
{
    int var =10; //an integer
    int *p;       // a pointer to an integer.  
    p = &var;    //make p point to var.  
    *p= 42;      //change the value of the integer that p points to (var). 
   printf("%i\n", var);  //did var change? print it to see!  
    return 0;
}


Task 1 (sort of):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

void display_value(const int &arg)
{
    std::cout << "lvalue address: " << &arg << ' ' << "rvalue: " << arg << '\n';
}

int main()
{
    int x = 512;
    int y = 1024;
    int z = 2048;

    display_value(x);
    display_value(y);
    display_value(z);

    x = 52;
    display_value(x);
    display_value(x = 5456788);

    return 0;
}


lvalue: 0x7ffeefbff458 rvalue: 512
lvalue: 0x7ffeefbff454 rvalue: 1024
lvalue: 0x7ffeefbff450 rvalue: 2048
lvalue: 0x7ffeefbff458 rvalue: 52
lvalue: 0x7ffeefbff458 rvalue: 5456788
5456788
Program ended with exit code: 0

In part 1, what does the prof mean by "left values" and "right values" Is it the address and the value as againtry has written? Or the left and right 8 bits of the value? Or something else?

How do I know I'm doing it right? What does it look like when you run it?

Your program should print the results of each task. For example, in task 2, you might print the value of flt_num after the update.
Hi dhayden, I am confused too. Unfortunately I can't contact my professor I'm not really sure about part 1 :/
Hello againtry! that is not a C language, right?
That's c++.
I see, but It has to be in C language :(
It has to be in C language

There are some online resources* for learning C:

https://www.learn-c.org/

OR

https://www.tutorialspoint.com/cprogramming/index.htm

Found with this meta-search:
https://duckduckgo.com/?q=c+language+online+tutorial&t=ffsb&ia=web

*I can't guarantee the fitness of the linked resources, I've only done a cursory glance at things.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int
main ()
{
  int x = 512;
  int y = 1024;
  int z = 2048;

  printf ("lvalue address: %p rvalue: %d\n", (void*)&x, x);
  printf ("lvalue address: %p rvalue: %d\n", (void*)&y, y);
  printf ("lvalue address: %p rvalue: %d\n", (void*)&z, z);

  x = 53;
  printf ("lvalue address: %p rvalue: %d\n", (void*)&x, x);
  printf ("lvalue address: %p rvalue: %d\n", (void*)&x, x = 5456788);

  return 0;
}



lvalue address: 0x7ffdf05b5414 rvalue: 512
lvalue address: 0x7ffdf05b5418 rvalue: 1024
lvalue address: 0x7ffdf05b541c rvalue: 2048
lvalue address: 0x7ffdf05b5414 rvalue: 53
lvalue address: 0x7ffdf05b5414 rvalue: 5456788


...Program finished with exit code 0
Topic archived. No new replies allowed.