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 char array[] and char *p in C

1 Answer

0 votes
#include <stdio.h> 

int main(void) 
{ 
    char arr[] = "c c++ java php";
	char *p = "c c++ java php";
	
    printf("1. %s\n", arr);
	printf("2. %s\n\n", p);
	
	printf("3. %d %d\n", (int)sizeof(arr), (int)sizeof(p));
	printf("4. %p %p\n", arr, p);
	printf("5. %p %p\n", &arr, &p);
	printf("6. %c %c %c\n\n", arr[0], p[0], *(p + 0));
	
	arr[0] = 'x';
	//p[0] = 'x'; Error
    printf("7. %s\n", arr);
	printf("8. %s\n\n", p);
	
	printf("9. %c %c %c\n\n", arr[13], p[13], *(p + 13)); // last char
	printf("10. %c %c %c\n\n", arr[14], p[14], *(p + 14)); // arr[14]=null (end string) p[14]=?
	printf("11. %c %c %c\n\n", arr[15], p[15], *(p + 15)); // arr[15]=? (out of index) p[15]=?
	
	
	//arr++; Error
	p++;
    printf("12. %s\n", arr);
	printf("13. %s\n\n", p);
	

     
    return 0; 
} 
   
   
   
/*
run:
   
1. c c++ java php
2. c c++ java php

3. 15 8
4. 000000000062FE10 0000000000404030
5. 000000000062FE10 000000000062FE08
6. c c c

7. x c++ java php
8. c c++ java php

9. p p p

10.

11.   1 1

12. x c++ java php
13.  c++ jaava php
 
*/

 



answered Aug 11, 2019 by avibootz
edited Aug 11, 2019 by avibootz

Related questions

1 answer 97 views
1 answer 156 views
1 answer 191 views
1 answer 186 views
1 answer 87 views
1 answer 95 views
...