Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

10,484 questions

14,262 answers

573 users

How to set and use int pointer to int array in C

1 Answer

0 votes
#include <stdio.h>

int main(int argc, char **argv) 
{ 
    int arr[5] = { 7, 9, 13, 18, 27 }, *p;
    
    p = arr;
    printf("arr address is: %X\n", arr);
    printf("p address is: %X\n\n", p);
    
    printf("arr[0] value is: %d\n", arr[0]);
    printf("*p value is: %d\n\n", *p);
    
    printf("arr[0] address is: %X\n", &arr[0]);
    printf("p + 0 address is: %X\n\n", p + 0);
    
    printf("arr[1] value is: %d\n", arr[1]);
    printf("*(p + 1) value is: %d\n\n", *(p + 1));
    
    arr[2] = 100;
    printf("arr[2] value is: %d\n", arr[2]);
    printf("*(p + 2) value is: %d\n\n", *(p + 2));   
    
    printf("arr[2] address is: %X\n", &arr[2]);
    printf("p + 2 address is: %X\n\n", p + 2);
    
    p = p + 2;
    printf("arr[2] value is: %d\n", arr[2]);
    printf("*p value is: %d\n\n", *p);   
    
    printf("arr[2] address is: %X\n", &arr[2]);
    printf("p address is: %X\n\n", p);
    
    *p = 2399;
    printf("arr[2] value is: %d\n", arr[2]);
    printf("*p value is: %d\n\n", *p);   
    
    printf("arr[2] address is: %X\n", &arr[2]);
    printf("p address is: %X\n\n", p);
    
    p = arr;
    for (int i = 0; i < 5; i++)
        printf("*(p + %d) value is: %d\n", i, *(p + i)); 
    for (int i = 0; i < 5; i++)
        printf("arr[%d] value is: %d\n", i, arr[i]); 
    for (int i = 0; i < 5; i++)
        printf("p[%d] value is: %d\n", i, p[i]); 
    for (int i = 0; i < 5; i++)
         // warning *p++ can point out of arrary memory
         printf("p address is: %X  *p++ value is: %d\n", p, *p++);
        
    p = arr; // p at the end of the loop is in out of array address &arr[5]
    
    while (p++ <= &arr[4]) // this is safer
        printf("p address is: %X  *p value is: %d\n", p, *p); 
        
    return 0;
}

/*
run:

arr address is: 28FEF8
p address is: 28FEF8

arr[0] value is: 7
*p value is: 7

arr[0] address is: 28FEF8
p + 0 address is: 28FEF8

arr[1] value is: 9
*(p + 1) value is: 9

arr[2] value is: 100
*(p + 2) value is: 100

arr[2] address is: 28FF00
p + 2 address is: 28FF00

arr[2] value is: 100
*p value is: 100

arr[2] address is: 28FF00
p address is: 28FF00

arr[2] value is: 2399
*p value is: 2399

arr[2] address is: 28FF00
p address is: 28FF00

*(p + 0) value is: 7
*(p + 1) value is: 9
*(p + 2) value is: 2399
*(p + 3) value is: 18
*(p + 4) value is: 27
arr[0] value is: 7
arr[1] value is: 9
arr[2] value is: 2399
arr[3] value is: 18
arr[4] value is: 27
p[0] value is: 7
p[1] value is: 9
p[2] value is: 2399
p[3] value is: 18
p[4] value is: 27
p address is: 28FEFC  *p++ value is: 7
p address is: 28FF00  *p++ value is: 9
p address is: 28FF04  *p++ value is: 2399
p address is: 28FF08  *p++ value is: 18
p address is: 28FF0C  *p++ value is: 27
p address is: 28FEFC  *p value is: 9
p address is: 28FF00  *p value is: 2399
p address is: 28FF04  *p value is: 18
p address is: 28FF08  *p value is: 27
p address is: 28FF0C  *p value is: 5

*/

 




answered Jun 27, 2015 by avibootz
...