Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

What is the difference between pointer to integer and pointer to an array of N integers in C

1 Answer

0 votes
#include <stdio.h>     

#define N 5

int main(void)
{
    int *p_int; // pointer to integer
	int (*p_arr)[N]; // pointer to an array of 5 integers
	int arr[N] = {3, 8, 6, 1, 9};
    
	p_int = arr; // pointer to arr[0]
	p_arr = &arr; // pointer to the whole array
    
    printf("p_int = %p, p_arr = %p\n", p_int, p_arr);
    printf("*p_int = %d, *p_arr[0] = %d\n", *p_int, *p_arr[0]);
    printf("*(p_int + 1) = %d, *(p_arr[0] + 1) = %d\n", *(p_int + 1), *(p_arr[0] + 1));
    
	p_int++;
	p_arr++;
	printf("p_int = %p, p_arr = %p\n", p_int, p_arr);
    printf("*p_int = %d, *p_arr[0] = %d\n", *p_int, *p_arr[0]);
    
    return 0;
}

/*
run:

p_int = 000000000022FE20, p_arr = 000000000022FE20
*p_int = 3, *p_arr[0] = 3
*(p_int + 1) = 8, *(p_arr[0] + 1) = 8
p_int = 000000000022FE24, p_arr = 000000000022FE34
*p_int = 8, *p_arr[0] = 0

*/

 



answered May 14, 2018 by avibootz

Related questions

2 answers 190 views
1 answer 87 views
1 answer 257 views
1 answer 191 views
1 answer 186 views
1 answer 87 views
1 answer 96 views
...