Question

Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise is to apply the knowledge of pointers and
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

C Program Code :

#include<stdio.h>

int main()
{
int a=5; //declare int variable and initialize it to some value
float b=7.2983; //declare float variable and initialize it to some value
double d=987321.53976; //declare double variable and initialize it to some value
char ch='Q'; //declare char variable and initialize it to some value

int *ptrToInt=&a; //declare a pointer variable to int and initialize it to point to int
float *ptrToFloat=&b; //declare a pointer variable to float and initialize it to pioint to ffloat
double *ptrToDouble=&d; //declare a pointer variable to double and initialize it to point to doouble
char *ptrToChar=&ch; //declare a pointer variable to char and initialize it to point to char


//print the value of all the four variables using pointers using dereferencing operator "*"
printf(" a = %d\n",*ptrToInt);
printf(" b = %f\n", *ptrToFloat);
printf(" d = %lf\n", *ptrToDouble);
printf(" ch = %c\n\n", *ptrToChar);

//print the addresses of memory locations of all 4 variales using the "&" operator
printf(" Address of a = %p\n",&a);
printf(" Address of b = %p\n", &b);
printf(" Address of d = %p\n", &d);
printf(" Address of ch = %p\n\n", &ch);

//print the content of the pointers using the pointer name
printf(" Content of ptrToInt = %p\n", ptrToInt);
printf(" Content of ptrToFloat = %p\n", ptrToFloat);
printf(" Content of ptrToDouble = %p\n", ptrToDouble);
printf(" Content of ptrToChar = %p\n\n", ptrToChar);

//print the address of the pointers using the pointer name
printf(" address of ptrToInt = %p\n", &ptrToInt);
printf(" address of ptrToFloat = %p\n", &ptrToFloat);
printf(" address of ptrToDouble = %p\n", &ptrToDouble);
printf(" address of ptrToChar = %p\n\n", &ptrToChar);

return 0;

}

Output :

Add a comment
Know the answer?
Add Answer to:
Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create 3 variables, (int, chart and float) and give give each of them some value. Print...

    Create 3 variables, (int, chart and float) and give give each of them some value. Print out the values. Then create a pointer for each variable and initialize the pointers. Then make them point to the memory address of the corresponding variables. Print out the pointers (=memory addresses where the pointers point to, use %p). Finally, print out the values that are in the memory locations where the pointers point to(so print out the values using the pointers, not the...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Please code in c 1. Write a program which does the following: Declare and initialize three...

    Please code in c 1. Write a program which does the following: Declare and initialize three pointers. One points to an integer, one points to a float and one points to a character variable Ask the user to enter appropriate values for these variables. Output the values to the screen by accessing the variables directly and then by using pointer references. Print the address of each variable. Modify the variables by performing any arithmetic operation only using pointer refer- ences...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which...

    C++ 29.5 Lab 29: Pointers Exercise: Before the main function, declare a struct called Movie which has title as a string and year as the integer as the member variables. Also before the main function, instantiate an object of Movie called m with initial value "Avengers" as title and year as 2019. Note m is the global variable and A in the title is uppercase. Also before the main function, declare another global variable which is a pointer to the...

  • Please Answer with C code. Will rate! Thanks 1) Write a program in C to demonstrate...

    Please Answer with C code. Will rate! Thanks 1) Write a program in C to demonstrate the use of &(address of) and *(value at address) operator. #include <stdio.h> void main() { int m=300; float fx = 300.60; char cht = 'z'; int *pt1; float *pt2; char *pt3; pt1= &m; pt2=&fx; pt3=&cht; a) print the address of variables (m, fx, cht) use & operation b) print the value at address of variables (m, Fx, cht) use & and * c) print...

  • Pointer tasks: ( In C++ ) 1. Write a program to store the address of a...

    Pointer tasks: ( In C++ ) 1. Write a program to store the address of a character variable and a float variable. • Print the address of both variables. • Print the values of variable using dereference operator. • Add 2 in float value using pointer. • Print address of character variable using reference operator. 2. Write a program that takes 5 elements in an array and print them in reverse order using pointers. 3. Write to program that takes...

  • (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major...

    (15 pts) Using the following example, int a, p=&a; *p = 20; Give the two major differences of * and & in the above example. (35 pts) Do the following in a program segment step by step. The next step uses the results of the previous step and earlier, using a single statement only and none for manual work. Declare an array called a of float of size 10 and initialize it to 2.5, -3.3, 5.9, 1.0, 3.5, 4.3, -7.3,...

  • I need help implementing Student(const char initId [], double gpa) without using pointers. I don't know...

    I need help implementing Student(const char initId [], double gpa) without using pointers. I don't know how to initialize this with the passed in gpa value. Using C++, implement the member function specified in student.h in the implementation file, student.cpp Student(const char initId[], double gpa) { } This function will initialize a newly created student object with the passed in value. Helpful info: From student.h class Student    {    public:           Student(const char initId[], double gpa);           bool isLessThanByID(const...

  • Using Structures in C language programming

    Write code to accomplish each of the following:Define a structure called Dove containing int variable dovNo and char array dovArr with values that may be as long as 25 characters (including the terminating null character).Define dove to be a synonym for the type struct Dove.Use Dove to declare variable a to be of type struct Dove, array b[ 10 ] to be of type struct Dove and variable ptr to be of type pointer to struct Dove.Read a dovNo and a dovArr from the keyboard into the individual members of variable a.Assign the member values of variable a to element 3 of array b.Assign the address...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT