Skip to main content

6. Arrays and Strings

 

Arrays

  • Arrays are used to store multiple values in a single variable instead of declaring separate variables for each value.
  • All the values stored in an array must be of same data type.
  • Internally array representation gets converted into pointer representation.
    int arr[5]; // Here arr is a constant integer pointer
    arr[idx] = *(arr + idx) = *(idx + arr) = idx[arr];
  1. What is the output of the following C code?
  2. #include <stdio.h>

    int main()
    {
        int arr[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++)
            printf("Address of %d idx element: %p\n", i, arr + i);
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>

    int main()
    {
        int arr[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++)
            printf("Value of arr[%d]: %d = %d = %d = %d\n", i, arr[i], *(arr + i), *(i + arr), i[arr]);
    }
    int arr[5];
    int *ptr = arr;

arr = Array name = constant pointer
ptr = Pointer name = variable pointer

Then we can't change the value of arr i.e., arr will point to a address which can't be changed, but we can change the value of ptr.

Valid statements    Invalid statements
ptr = &num;    arr = &num;
ptr++;    arr++;
ptr = ptr - 1;    arr = arr - 1;

Pointer to an Array

    int arr[5] = {1,2,3,4,5};
    int (*arrPtr)[5] = &arr;

Now, arrPtr will behave same as arr, but we can change it unlike arr.

  1. What is the output of the following C code?
  2. #include <stdio.h>

    int main()
    {
        int a[10];
        printf("%d", *a + 1 - *a + 3);
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>

    char *getString()
    {
        char str[] = "Deepak Chandra Sharma";
        return str;
    }

    int main()
    {
        printf("%s", getString());
        return 0;
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>

    int main()
    {
        int arr[] = {1, 2, 3};
        int *p = arr;
        if (&p == arr)
            printf("Same");
        else
            printf("Not same");
    }
  7. What is the output of the following C code?
  8. #include <stdio.h>

    int main()
    {
        int arr[] = {1, 2, 3};
        int *p = arr;
        if (p == arr)
            printf("Same");
        else
            printf("Not same");
    }

Strings

  • a string is a sequence of characters that are stored in contiguous memory locations.
  • Strings in C are represented as arrays of characters, terminated by a null character '\0'.
  • char str[] = "abc";
  • str is a string.
  • char *strPtr = "abc";
  • strPtr is a string constant or string literal.
  1. What is the output of the following C code?
  2. #include <stdio.h>

    int main()
    {
        char str[] = "abc";
        str[1] = '1';
        printf(str);
        char *ptr = "abc";
        ptr[1] = 'p';
        printf(ptr);
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>

    int main()
    {
        char arr[] = "deep";
        char *ptr = arr;
        while (*ptr != '\0')
            ++*ptr++;
        printf("%s %s", arr, ptr);
    }
  5. What is the output of the following C code?
  6. #include <stdio.h>

    int main()
    {
        char str[] = "abc" "pqr""abc";
        printf(str);
    }
  7. What is the output of the following C code?
  8. #include <stdio.h>

    int main()
    {
        char str[] = "geeks\nknowledge";
        char *ptr1, *ptr2;
        ptr1 = &str[3];
        ptr2 = str + 5;
        printf("%c", ++*str - --*ptr1 + *ptr2 + 2);
        printf("%s", str);
    }
  9. What is the output of the following C code?
  10. #include <stdio.h>

    int main()
    {
        char str[] = "Deepak";
        char *s1 = str, *s2 = str;
        int i;
        for (i = 0; i < 7; i++)
        {
            printf("%c", *str);
            ++s1;
        }
        for (i = 0; i < 6; i++)
        {
            printf("%c", *s2);
            ++s2;
        }
    }
char str[] = {'a', 'b', 'c'};

If we don't add '\0' as the last element of character array like above, then our string will get appended by some garbage value. But if we also give the size while initializing, then if there are 3 elements (excluding '\0') and we give size 4 or more, then it will be fine, but if we give size less than 4, it will add up some garbage values.

But if we declare a string using double quotes, then there is no need to add '\0' at the end, compiler automatically adds that.


Prev Next

Comments