Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

pointer arithmetic in c

#include <stdio.h>

int main()
{ // pointer Arithmetic
    printf("
Pointer Arithmetic
");
    int i, *ptr;
    int num[] = {10, 100, 200};
    // assign array address to pointer
    ptr = num;
    // value of ptr = address of num
    printf("
    Value of ptr -----  %x
", ptr);

    for (i = 0; i < 3; i++)
    {

        printf("    Address of num[%d] = %x
", i, ptr);
        printf("    Value of num[%d] = %d
", i, *ptr);
        ptr++; // next address space  Jumps by 4 due to the size of an int in memory.
    }
	
    // String Array arithmetic
   	// I'm not recommending you do this 
    printf("

String Arithmetic

");
    char *str[12] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"};

    printf("    Array value at index 11 is %s
", str[11]);
    // address of array at index
    printf("    Address of array at index 0 %x
", str[0]);
    // address of array
    printf("    Address of array %x
", *str);
    // increases the memory address by one.
    str[0]++;
    // address of array
    printf("    Address of array after array has been incremented by one %x
", *str);

    return 0;
 
PREVIOUS NEXT
Tagged: #pointer #arithmetic
ADD COMMENT
Topic
Name
6+6 =