Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
build
3 changes: 2 additions & 1 deletion experiment/aim.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Every time a variable is declared, some bytes are allocated to that variable depending on its datatype. For example, int takes 4 bytes, a char takes only one byte. Arrays allow allocation of even larger number of storage space or "bytes". Each of these bytes has a distinct address in the main memory. Pointer is a datatype which can store this address. Hence a pointer can serve as a "reference" to the data contained other variables. This has some useful applications, most importantly, allowing users to modify data from within a function without needing to make a copy of the data, and also allowing the user to make "linked" data structures.
When a variable is declared, a certain number of bytes are allocated to it depending on its datatype (e.g., an int takes 4 bytes, a char takes 1 byte). Arrays allow allocation of larger blocks of memory. Each byte in memory has a unique address. A pointer is a datatype that can store the address of a memory location, allowing it to reference data contained in other variables. Pointers are useful for many applications, such as allowing functions to modify data directly (without making a copy) and enabling the creation of linked data structures.

Aim of this experiment is to understand the concept of pointers in C programming, their declaration, usage, and applications in referencing and manipulating memory addresses.
134 changes: 107 additions & 27 deletions experiment/posttest.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,107 @@
[
{"question":"1. Pointer is a :",
"answers":{
"a":"A keyword used to create variables",
"b":"A variable that stores address of an instruction",
"c":"A variable that stores address of other variable",
"d":"All of the above"
},
"correctAnswer":"c"},

{"question":"2. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?:",
"answers":{
"a":".",
"b":"&",
"c":"*",
"d":"->"
},
"correctAnswer":"d"},

{"question":"3.The name of the array is a pointer to the _________ element of the array. ",
"answers":{
"a":"first",
"b":"second"
},
"correctAnswer":"a"}

]
{
"version": 2,
"questions": [
{
"question": "1. Pointer is a :",
"answers": {
"a": "A keyword used to create variables",
"b": "A variable that stores address of an instruction",
"c": "A variable that stores address of other variable",
"d": "All of the above"
},
"correctAnswer": "c",
"explanations": {
"a": "Incorrect, a keyword is not a pointer.",
"b": "Incorrect, a pointer can store the address of any variable, not just instructions.",
"c": "Correct. A pointer is a variable that stores the address of another variable.",
"d": "Incorrect, not all of the above are correct."
},
"difficulty": "beginner"
},
{
"question": "2. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?",
"answers": {
"a": ".",
"b": "&",
"c": "*",
"d": "->"
},
"correctAnswer": "d",
"explanations": {
"a": "Incorrect, . is used for direct structure access, not through a pointer.",
"b": "Incorrect, & is the address-of operator.",
"c": "Incorrect, * is the dereference operator.",
"d": "Correct. -> is used to access members of a structure through a pointer."
},
"difficulty": "intermediate"
},
{
"question": "3. The name of the array is a pointer to the _________ element of the array.",
"answers": {
"a": "first",
"b": "second",
"c": "last",
"d": "middle"
},
"correctAnswer": "a",
"explanations": {
"a": "Correct. The name of the array points to the first element.",
"b": "Incorrect, it does not point to the second element.",
"c": "Incorrect, it does not point to the last element.",
"d": "Incorrect, it does not point to the middle element."
},
"difficulty": "beginner"
},
{
"question": "4. Which operator is used to get the address of a variable in C?",
"answers": {
"a": "*",
"b": "&",
"c": "->",
"d": "."
},
"correctAnswer": "b",
"explanations": {
"a": "Incorrect, * is used for dereferencing a pointer.",
"b": "Correct. & is the address-of operator in C.",
"c": "Incorrect, -> is used to access members of a structure through a pointer.",
"d": "Incorrect, . is used to access members of a structure directly."
},
"difficulty": "beginner"
},
{
"question": "5. What is the output of the following code?\nint x = 10;\nint *p = &x;\nprintf(\"%d\", *p);",
"answers": {
"a": "10",
"b": "0",
"c": "Address of x",
"d": "Compilation error"
},
"correctAnswer": "a",
"explanations": {
"a": "Correct. 10 is printed because *p dereferences the pointer to x.",
"b": "Incorrect, 0 is not printed.",
"c": "Incorrect, the address of x is not printed.",
"d": "Incorrect, there is no compilation error."
},
"difficulty": "intermediate"
},
{
"question": "6. Which of the following is NOT a valid use of pointers?",
"answers": {
"a": "Dynamic memory allocation",
"b": "Accessing array elements",
"c": "Storing function addresses",
"d": "Directly storing string literals"
},
"correctAnswer": "d",
"explanations": {
"a": "Incorrect, pointers are used for dynamic memory allocation.",
"b": "Incorrect, pointers can be used to access array elements.",
"c": "Incorrect, pointers can store function addresses.",
"d": "Correct. String literals are stored as constant character arrays, not directly in pointers."
},
"difficulty": "advanced"
}
]
}
85 changes: 69 additions & 16 deletions experiment/pretest.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
[
{"question":"1. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]",
"answers":{
"a":"((((a+i)+j)+k)+l)",
"b":" *(*(*(*(a+i)+j)+k)+l)",
"c":"(((a+i)+j)+k+l)",
"d":"((a+i)+j+k+l)"
},
"correctAnswer":"b"},
{"question":"2. NULL pointer points to the 0th memory address:",
"answers":{
"a":"True",
"b":"False"
},
"correctAnswer":"a"}
]
{
"version": 2,
"questions": [
{
"question": "1. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]?",
"answers": {
"a": "((((a+i)+j)+k)+l)",
"b": "*(*(*(*(a+i)+j)+k)+l)",
"c": "(((a+i)+j)+k+l)",
"d": "((a+i)+j+k+l)"
},
"correctAnswer": "b",
"explanations": {
"a": "Incorrect, this does not dereference at each dimension.",
"b": "Correct. This is the correct pointer expression for a 4D array element.",
"c": "Incorrect, this is missing dereferencing at each level.",
"d": "Incorrect, this is missing dereferencing and proper grouping."
},
"difficulty": "advanced"
},
{
"question": "2. NULL pointer points to the 0th memory address:",
"answers": {
"a": "True",
"b": "False"
},
"correctAnswer": "a",
"explanations": {
"a": "Correct. In C, a NULL pointer is defined as a pointer with value 0, which means it points to the 0th memory address.",
"b": "Incorrect, in C, a NULL pointer is defined as a pointer with value 0."
},
"difficulty": "beginner"
},
{
"question": "3. Which operator is used to get the address of a variable in C?",
"answers": {
"a": "*",
"b": "&",
"c": "->",
"d": "."
},
"correctAnswer": "b",
"explanations": {
"a": "Incorrect, * is used for dereferencing a pointer.",
"b": "Correct. & is the address-of operator in C.",
"c": "Incorrect, -> is used to access members of a structure through a pointer.",
"d": "Incorrect, . is used to access members of a structure directly."
},
"difficulty": "beginner"
},
{
"question": "4. Which of the following is NOT a valid use of pointers?",
"answers": {
"a": "Dynamic memory allocation",
"b": "Accessing array elements",
"c": "Storing function addresses",
"d": "Directly storing string literals"
},
"correctAnswer": "d",
"explanations": {
"a": "Incorrect, pointers are used for dynamic memory allocation.",
"b": "Incorrect, pointers can be used to access array elements.",
"c": "Incorrect, pointers can store function addresses.",
"d": "Correct. String literals are stored as constant character arrays, not directly in pointers."
},
"difficulty": "intermediate"
}
]
}
12 changes: 6 additions & 6 deletions experiment/procedure.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
**Call By Value**

1. Press start to start the experiment.
2. Press next to see the execution of the code.
1. Press Start to start the experiment.
2. Press Next to see the execution of the code.
3. Relavant line in the code is shown here.
4. The output of the code is shown in the right.

**Call By Reference**

1. Press start to start the experiment.
2. Press next to see the execution of the code.
3. The output of the code is shown in the right.
4. You can stop the code using stop button.
1. Press Start to start the experiment.
2. Press Next to see the execution of the code.
3. Relavant line in the code is shown here.
4. The output of the code is shown in the right.

#### Procedure

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<div id = 'codeContentBP3' >&emsp;&emsp;int A = 10;</div>
<div id = 'codeContentBP4' >&emsp;&emsp;printf("Value of A is %d\n",A);</div>
<div id = 'codeContentBP5' >&emsp;&emsp;printf("Address of A is %d\n",&A);</div>
<div id = 'codeContentBP6' >&emsp;&emsp;int *P</div>
<div id = 'codeContentBP6' >&emsp;&emsp;int *P;</div>
<div id = 'codeContentBP7' >&emsp;&emsp;P = &A;</div>
<div id = 'codeContentBP8' >&emsp;&emsp;printf("Value of P is %d\n",P);</div>
<div id = 'codeContentBP9' >&emsp;&emsp;printf("Address of P is %d\n",&P);</div>
Expand Down
61 changes: 27 additions & 34 deletions experiment/theory.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,54 @@
A pointer is a programming language data type which can store memory addresses of other variables. A pointer variable corresponding to any data type can be declared by using * before the name of the pointer.
Pointers are a fundamental concept in C programming that allow variables to store memory addresses of other variables. This enables direct access and manipulation of memory, which is essential for efficient programming and advanced data structures.

```
int *pointer1_1,*pointer2_i, var_i=5;

```

This would declare 2 integer pointers meant to store references(memory address) to variables of integer data type. Note that var_i is just a normal integer variable. Alternatively, pointers can also be declared as:
#### Declaring and Using Pointers

A pointer is declared by placing an asterisk (\*) before its name. For example:

```
int* pointer1_1,pointer2_i;
int var_i=5;
int *ptr;
```

This declares a pointer to an integer. You can assign the address of a variable to a pointer using the address-of operator (&):

```
int var = 5;
ptr = &var;
```

The & operator can be used with any variable to get its memory address. So, writing
Now, `ptr` holds the address of `var`. To access the value stored at that address (dereferencing), use the asterisk:

```
pointer1_i=&var_i;
```

would assign the memory address of var_i to pointer1_i.
printf("%d\n", *ptr); // prints 5
```

Figure explaining the working of the statement: a=&b;
#### Pointers and Arrays

<img src="images/pointers.png">

The * operator is used to access the values stored at a given address. So, writing
Arrays are closely related to pointers. The name of an array acts as a constant pointer to its first element. For example:

```
printf("%d\n",*pointer1_i);

int arr[100];
```

would print 5 on the console. This process of accessing the value stored at a given position is known as dereferencing a pointer. Similarly, to store references to character or float variables one can define char or float type pointers.
Here, `arr` is equivalent to a pointer to the first element. Accessing `arr[3]` is the same as `*(arr + 3)`.

A pointer can be used to allocate memory in the runtime using the malloc() function, defined in stdlib.h, by doing dynamic memory allocation. Writing
#### Dynamic Memory Allocation

```
int *ptr=(int *)malloc(20*(sizeof(int));
Pointers are essential for dynamic memory allocation. Using the `malloc()` function from `stdlib.h`, you can allocate memory at runtime:

```
int *ptr = (int *)malloc(20 * sizeof(int));
```

will allocate a space 20 integer variables and store the address of the first byte in ptr. An array, infact, is just a constant pointer to which allocation is done automatically. Hence, writing
This allocates space for 20 integers and stores the address in `ptr`.

```
int arr[100];
#### Pointer Arithmetic

```
Pointers support arithmetic operations. Adding 1 to an integer pointer moves it to the next integer (skipping 4 bytes), while adding 1 to a char pointer moves it by 1 byte. This is useful for iterating through arrays and managing memory.

is equivalent to writing
#### Applications of Pointers

```
const int* arr=(int *)malloc(100*sizeof(int));
```
Pointers allow functions to modify variables directly, enable the creation of dynamic data structures like linked lists, and are crucial for efficient memory management in C.

So, writing *arr will give the value stored at arr[0].
<img src="images/pointers.png">

Arithmatical operations like addition and subtraction can be performed to a pointer. The nature of these arithmatic operations is what distinguishes an integer pointer from, say, a character pointer, which otherwise just store memory addresses for another variable. Adding one to an interger pointer makes to point to the next integer, and hence, it skips 4 bytes. Adding one to a character pointer makes it to point to the next character, and hence, it skips only 1 byte. Hence, for the array arr, writing arr[3] is equivalent to writing *(arr+3). Both refer to the value stored in the 4th cell of the array.
Arithmatical operations like addition and subtraction can be performed to a pointer. The nature of these arithmatic operations is what distinguishes an integer pointer from, say, a character pointer, which otherwise just store memory addresses for another variable. Adding one to an interger pointer makes to point to the next integer, and hence, it skips 4 bytes. Adding one to a character pointer makes it to point to the next character, and hence, it skips only 1 byte. Hence, for the array arr, writing arr[3] is equivalent to writing \*(arr+3). Both refer to the value stored in the 4th cell of the array.
Loading
Loading