-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07oct_2.c
33 lines (27 loc) · 1.71 KB
/
07oct_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e'};
int *ptr = arr;
printf("%c", *(ptr + 1));
return 0;
}
/*
-This C code snippet is designed to print the second character of a character array arr.
-The array arr is declared and initialized with five characters: 'a', 'b', 'c', 'd', and 'e'.
-A pointer ptr of type int is declared and assigned the address of the first element of the character array arr.
-However, this is incorrect and can lead to undefined behavior because ptr is an int pointer, but it's assigned the address of a char array.
-A char pointer should be used instead.
-The printf statement is used to print the second character of the array.
-The expression ptr + 1 increments the pointer ptr by one element of its type (int).
-Since ptr is an int pointer, incrementing it by one moves the pointer to the next int location, not the next char location.
-This is incorrect and can lead to undefined behavior. The * operator dereferences the pointer, accessing the value at the incremented location.
-The %c format specifier in printf expects a char argument, but the expression *(ptr + 1) evaluates to an int value due to the incorrect pointer type.
-To fix the issues in the original code, you should use a char pointer instead of an int pointer.
-With this correction, the code will correctly print the second character 'b' of the array arr.
*/
/*
# The size of a pointer variable in C depends on the architecture of the system. In most modern systems, a pointer is typically 4 bytes (32 bits) or 8 bytes (64 bits) in size.
- On a 32-bit system, the size of a pointer is usually 4 bytes (32 bits).
- On a 64-bit system, the size of a pointer is usually 8 bytes (64 bits).
*/