Skip to content

Commit deffd50

Browse files
committed
Create problem14
1 parent eb534ea commit deffd50

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

problem14

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//Solved by Sagar M, Dept of CSE
2+
3+
4+
C Array
5+
6+
An array is a collection of same type of elements which are sheltered under a common name.
7+
8+
<type-of-array> <name-of-array> [<number of elements in array>];
9+
10+
arr[i] = i; // Initializing each element seperately
11+
12+
Accessing Values in an Array:
13+
14+
arr[i] = i; // Initializing each element separately
15+
16+
int j = arr[5]; // Accessing the 6th element of integer array arr and assigning
17+
18+
struct st st_arr[3]; // Declare an array of 3 structure objects
19+
20+
struct st st_obj0; // first structure object
21+
22+
struct st st_obj1; //Second structure object
23+
24+
struct st st_obj2; // Third structure object
25+
26+
st_arr[0] = st_obj0; // Initializing first element of array with first
27+
28+
st_arr[1] = st_obj1; // Initializing second element of array with second
29+
30+
st_arr[2] = st_obj2; // Initializing third element of array with third
31+
32+
printf("\n First Element of array has values of a = [%d] and c = [%c]\n",
33+
34+
printf("\n Second Element of array has values of a = [%d] and c = [%c]\n",
35+
36+
printf("\n Third Element of array has values of a = [%d] and c = [%c]\n",
37+
38+
Pointers in C Programming language is very powerful. Combining pointers with arrays can be very
39+
40+
A Simple C Program using Arrays
41+
42+
Consider this simple program that copies a string into an array and then changes one of its
43+
44+
char arr[4];// for accommodating 3 characters and one null '\0' byte.
45+
46+
char *ptr = "abc"; //a string containing 'a', 'b', 'c', '\0'
47+
48+
memset(arr, '\0', sizeof(arr)); //reset all the bytes so that none of the
49+
50+
strncpy(arr,ptr,sizeof("abc")); // Copy the string "abc" into the array arr
51+
52+
printf("\n %s \n",arr); //print the array as string
53+
54+
arr[0] = 'p'; // change the first character in the array
55+
56+
printf("\n %s \n",arr);//again print the array as string
57+
58+
I think the program is self explanatory as I have added plenty of comments. The output of the above
59+
60+
In C, an array is a fixed-size region of contiguous storage containing multiple
61+
62+
objects, one after the other. This array is an "object" in the meaning which C gives to the
63+
64+
word - basically just some memory that represents something. An object could just be
65+
66+
You can distinguish slightly between array objects, and array types. Often people
67+
68+
use array objectswhich are allocated with malloc, and used via a pointer to the first element.
69+
70+
But C does also have specific types for arrays of different sizes, and also for variable-
71+
72+
length-arrays, whose size is set when they are created. VLAs have a slightly misleading
73+
74+
name: the size is only "variable" in the sense that it isn't fixed at compile time. It can't
75+
76+
change during the lifetime of the object.
77+
78+
So, when I say an array is fixed-size I mean that the size cannot change once the
79+
80+
array is created, and this includes VLAs. There is realloc, which logically returns a pointer
81+
82+
to a new array that replaces the old one, but can sometimes return the same address
83+
84+
passed in, having changed the size of the array in place. realloc operates on memory
85+
86+
allocations, not on arrays in general.
87+
88+
That's what an array is. The C programming language doesn't define anything called
89+
90+
a list. Can't really compare something which is well defined, with something that isn't
91+
92+
defined ;-) Usually "list" would mean a linked list, but in some contexts or in other languages
93+
94+
For that matter, in other languages "array" could mean other things, although I can't
95+
96+
immediately think of a language where it means anything very different from a C array

0 commit comments

Comments
 (0)