0% found this document useful (0 votes)
11 views3 pages

Solution ST-1

The document defines and initializes various C/C++ variable types including pointers, arrays, references, and constants. It then prints the values of each variable to demonstrate their usage and initialization.

Uploaded by

farsamuels183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Solution ST-1

The document defines and initializes various C/C++ variable types including pointers, arrays, references, and constants. It then prints the values of each variable to demonstrate their usage and initialization.

Uploaded by

farsamuels183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

#include <stdio.h>

int main() {
//1. Pointer to a character
char ch = 'A';
char *ptrChar = &ch;

//2. Array of 10 integers


int arrInt[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

//3. Reference to an array of 10 integers


int (&refArrInt)[10] = arrInt;

//4. Pointer to an array of character strings


char *strArr[] = {"Hello", "World", "C", "Programming"};
char **ptrStrArr = strArr;

//5. Pointer to a pointer to a character


char c = 'B';
char *ptrChar2 = &c;
char **ptrPtrChar = &ptrChar2;

//6. Constant integer


const int constInt = 42;

//7. Pointer to a constant integer


const int *ptrConstInt = &constInt;

//8. Constant pointer to an integer


int num = 100;
int *const constIntPtr = &num;

//9. Constant pointer to a constant double


const double constDbl = 3.14159;
const double *const constPtrConstDbl = &constDbl;

//10. Printing values to verify initialization


printf("Pointer to a character: %c\n", *ptrChar);
printf("Array of 10 integers: %d\n", arrInt[0]);
printf("Reference to an array of 10 integers: %d\n", refArrInt[1]);
printf("Pointer to an array of character strings: %s\n", *ptrStrArr);
printf("Pointer to a pointer to a character: %c\n", **ptrPtrChar);
printf("Constant integer: %d\n", constInt);
printf("Pointer to a constant integer: %d\n", *ptrConstInt);
printf("Constant pointer to an integer: %d\n", *constIntPtr);
printf("Constant pointer to a constant double: %f\n", *constPtrConstDbl);

return 0;
}
2.
#include <iostream>
int main()
{
const char* str1 = "We love C++ programming\n";
char str2[100];
char *p = str2; // *p points the beginning of the array str2. It's used to traverse str2 while
copying characters from str1.
while(*p++ = *str1++){} // This while loop terminates only when the NULL character of
str1 is encountered. It also copies characters from str1 to str2 one
by one.
std::cout << str2;
}
Explanation:

3.
#include <iostream>

const char* createString()


{
return "Practice makes a man perfect";
}
int* createInt()
{
int x = 100;
return &x;
}
int main()
{
const char *str = createString(); // The function returns a pointer of a string literal, which is
statically allocated in memory. So, after the execution of
the function createString(), the statically allocated memory
of the string literal is not erased.
std::cout << "string = " << str << std::endl; // It prints the expected string "Practice makes a
man perfect"
int *ip = createInt(); // This function returns the address of an integer-type local variable x.
After the execution of the function, the memory allocated for variable x will
be destroyed. So its behavior is undefined, sometimes it shows some
errors, or sometimes shows some garbage value.
std::cout << "integer = " << *ip << std::endl;
}
4.
#include<iostream>
#include<stdio.h>
int main()
{
int m,n;
printf("Enter two number:");
scanf("%d, %d",&m,&n);
if(m ^ n)
printf("both are not equal");
else
printf("both are equal");
}
5.
#include<iostream>
int main()
{
int x = 10, y = 5;
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}

You might also like