Arrays and Strings Flashcards
(8 cards)
What is the syntax to declare a one-dimensional integer array named data that can hold 10 elements?
int data[10];
How do you initialize a one-dimensional array numbers with the values 1, 2, 3, 4, 5 during declaration?
int numbers[] = {1, 2, 3, 4, 5};
How do you read 5 integer elements into an array M using a for loop and scanf()?
int M[5], i;
for (i = 0; i < 5; i++) {
printf(“Enter element %d: “, i + 1);
scanf(“%d”, &M[i]);
}
What is the syntax for passing an entire one-dimensional array named myArray and its size to a function named processArray?
Function Prototype: void processArray(int arr[] or int* arr, int size);
Function Call: processArray(myArray, arraySize);
How do you declare and initialize a 2D integer array (matrix) with 2 rows and 3 columns?
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
What is a C string? How is it terminated?
A sequence of characters stored in a one-dimensional character array, terminated by a null character (\0).
Name two string manipulation functions from <string.h> and their purpose.</string.h>
strlen(string): Returns the length of the string (excluding \0).
strcpy(destination, source): Copies the source string to destination.
strcat(destination, source): Concatenates source to the end of destination.
strcmp(string1, string2): Compares two strings.
How do you declare a string variable myString that can hold up to 20 characters (including the null terminator)?
char myString[20];