0% found this document useful (0 votes)
161 views

PPS Unit-8 Pointers 2marks Ques With Ans

Pointers are variables that store memory addresses of other variables. They are declared with a data type followed by an asterisk, such as int *ptr. The address of a variable can be obtained using the & operator. Pointers can be used to access and modify the value of the variable located at the address they are pointing to using the unary * operator. Pointers allow for efficient handling of arrays and structures in C and enable dynamic memory allocation. Common pointer types include null pointers, which contain a value of 0, and void pointers, which do not have a specified data type. Pointer arithmetic and expressions can be performed on pointers to arrays.

Uploaded by

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

PPS Unit-8 Pointers 2marks Ques With Ans

Pointers are variables that store memory addresses of other variables. They are declared with a data type followed by an asterisk, such as int *ptr. The address of a variable can be obtained using the & operator. Pointers can be used to access and modify the value of the variable located at the address they are pointing to using the unary * operator. Pointers allow for efficient handling of arrays and structures in C and enable dynamic memory allocation. Common pointer types include null pointers, which contain a value of 0, and void pointers, which do not have a specified data type. Pointer arithmetic and expressions can be performed on pointers to arrays.

Uploaded by

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

Pointers

2 Marks Questions
Unit- 8
Q1. What are Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the
memory location. Like any variable or constant, you must declare a pointer before using it to
store any variable address. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and var-name is the name
of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for
multiplication. However, in this statement the asterisk is being used to designate a variable as
a pointer. Take a look at some of the valid pointer declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
The actual data type of the value of all pointers, whether integer, float, character, or otherwise,
is the same, a long hexadecimal number that represents a memory address. The only
difference between pointers of different data types is the data type of the variable or constant
that the pointer points to.
Q 2 Explain Address in C language.
If you have a variable var in your program, &var will give you its address in the memory.
We have used address numerous times while using the scanf() function.

scanf("%d", &var);

Here, the value entered by the user is stored in the address of var variable. Let's take a working
example.

#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);

// Notice the use of & before var


printf("address of var: %p", &var);
return 0;
}

Output

var: 5
address of var: 2686778
Q3 How to Use Pointers?
There are a few important operations, which we will do with the help of pointers very
frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer
and (c) finally access the value at the address available in the pointer variable. This is done by
using unary operator * that returns the value of the variable located at the address specified by
its operand. The following example makes use of these operations −
#include <stdio.h>

int main () {

int var = 20; /* actual variable declaration */


int *ip; /* pointer variable declaration */

ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip );

return 0;
}
When the above code is compiled and executed, it produces the following result −
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

Q4 Define NULL Pointers.


Ans It is always a good practice to assign a NULL value to a pointer variable in case you do
not have an exact address to be assigned. This is done at the time of variable declaration. A
pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries.
Consider the following program −
#include <stdio.h>

int main () {

int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;
}
When the above code is compiled and executed, it produces the following result −
The value of ptr is 0
In most of the operating systems, programs are not permitted to access memory at address 0
because that memory is reserved by the operating system. However, the memory address 0 has
special significance; it signals that the pointer is not intended to point to an accessible memory
location. But by convention, if a pointer contains the null (zero) value, it is assumed to point
to nothing.
To check for a null pointer, you can use an 'if' statement as follows −
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */

Q 5 Declaring a pointer.
Ans Like variables, pointers have to be declared before they can be used in your program.
Pointers can be named anything you want as long as they obey C's naming rules. A pointer
declaration has the following form.
data_type * pointer_variable_name;
Here,
 data_type is the pointer's base type of C's variable types and indicates the type of the
variable that the pointer points to.
 The asterisk (*: the same asterisk used for multiplication) which is indirection operator,
declares a pointer.
Let's see some valid pointer declarations
int *ptr_thing; /* pointer to an integer */
int *ptr1,thing;/* ptr1 is a pointer to type integer and thing is an integer variable */
double *ptr2; /* pointer to a double */
float *ptr3; /* pointer to a float */
char *ch1 ; /* pointer to a character */
float *ptr, variable;/*ptr is a pointer to type float and variable is an ordinary float variable */

Q 6 How to Initialize a pointer.


After declaring a pointer, we initialize it like standard variables with a variable address. If
pointers are not uninitialized and used in the program, the results are unpredictable and
potentially disastrous.
To get the address of a variable, we use the ampersand (&)operator, placed before the name of
a variable whose address we need. Pointer initialization is done with the following syntax.
pointer = &variable;
A simple program for pointer illustration is given below:
#include <stdio.h>
int main()
{
int a=10; //variable declaration
int *p; //pointer variable declaration
p=&a; //store address of variable a in pointer p
printf("Address stored in a variable p is:%x\n",p); //accessing the address
printf("Value stored in a variable p is:%d\n",*p); //accessing the value
return 0;
}
Output:
Address stored in a variable p is:60ff08
Value stored in a variable p is:10

Q 7 Discuss types of a pointer.


Ans Null pointer
We can create a null pointer by assigning null value during the pointer declaration. This
method is useful when you do not have any address assigned to the pointer. A null pointer
always contains value 0.
Following program illustrates the use of a null pointer:
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output:
The value inside variable p is:
0
Void Pointer
In C programming, a void pointer is also called as a generic pointer. It does not have any
standard data type. A void pointer is created by using the keyword void. It can be used to store
an address of any variable.
Following program illustrates the use of a void pointer:
#include <stdio.h>
int main()
{
void *p = NULL; //void pointer
printf("The size of pointer is:%d\n",sizeof(p));
return 0;
}
Output:
The size of pointer is:4
Wild pointer
A pointer is said to be a wild pointer if it is not being initialized to anything. These types of
pointers are not efficient because they may point to some unknown memory location which
may cause problems in our program and it may lead to crashing of the program. One should
always be careful while working with wild pointers.
Following program illustrates the use of wild pointer:
#include <stdio.h>
int main()
{
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}
Output
timeout: the monitored command dumped core
sh: line 1: 95298 Segmentation fault timeout 10s main
Other types of pointers in 'c' are as follows:
 Dangling pointer
 Complex pointer
 Near pointer
 Far pointer
 Huge pointer
Q8 Explain Pointer Expressions and Pointer Arithmetic.
Ans A limited set of arithmetic operations can be performed on pointers. A pointer may be:
 incremented ( ++ )
 decremented ( — )
 an integer may be added to a pointer ( + or += )
 an integer may be subtracted from a pointer ( – or -= )
Pointer arithmetic is meaningless unless performed on an array.
Q 9 Classify Strings as pointers.
Ans We've already discussed strings, but now we can dive in a bit deeper and understand what
strings in C really are (which are called C-Strings to differentiate them from other strings when
mixed with C++)
The following line:
char * name = "John";
does three things:
1. It allocates a local (stack) variable called name, which is a pointer to a single character.
2. It causes the string "John" to appear somewhere in the program memory (after it is
compiled and executed, of course).
3. It initializes the name argument to point to where the J character resides at (which is
followed by the rest of the string in the memory).
If we try to access the name variable as an array, it will work, and will return the ordinal value
of the character J, since the name variable actually points exactly to the beginning of the string.
Since we know that the memory is sequential, we can assume that if we move ahead in the
memory to the next character, we'll receive the next letter in the string, until we reach the end
of the string, marked with a null terminator (the character with the ordinal value of 0, noted
as \0).

Q 10 Benefits of using pointers.


Below we have listed a few benefits of using pointers:
1. Pointers are more efficient in handling Arrays and Structures.
2. Pointers allow references to function and thereby helps in passing of function as
arguments to other functions.
3. It reduces length of the program and its execution time as well.
4. It allows C language to support Dynamic Memory management.
In the next tutorial we will learn syntax of pointers, how to declare and define a pointer, and
using a pointer. See you in the next tutorial.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy