[C Advanced] Look at arrays and pointers from 3 small examples

Array pointer and offset

First look at the first piece of code

int a[5] = {0, 1, 2, 3, 4};
printf("a:\t%p\n", a);
printf("&a:\t%p\n", &a);
printf("a+1:\t%p\n", a+1);
printf("&a+1:\t%p\n", &a+1);
  1. Print the name of the array, which is the identifier a of the array, the result is 0133FD80 , indicating the first address of the array
  2. Add the address character & in front of the array name, the result is 0133FD80 , which also indicates the first address of the array
  3. Add 1 to the print array name, and the result is 0133FD80 . Because int occupies 4 bytes, adding 1 to the first address of the array does not mean that the address value is directly increased by 1, but the address offset is 4 bytes, pointing to the first address of the next element of the array.
    4 . The array name takes the address symbol & and then adds 1, and the result is 0133FD94 , which is an increase of 20, which is the size of the space occupied by the 5 elements in the array, indicating that the whole array is first used & and then 1 is added to offset the entire array. Large address space

Character array

Look at the second piece of code

char *cp;
char *cp1;
cp =(char *) malloc(strlen("hello") + 1);
if(NULL == cp)
{
	exit(1);
}
strcpy(cp, "hello");
cp1 = cp;
printf("%s\n", &cp[0]);
printf("%s\n", cp1);
free(cp);
  1. The strlen () function returns the effective length of a string, and the sizeof () function returns the actual space occupied by a string, including the trailing \ 0 character, so the size when assigning an address to a character pointer Need to add 1
  2. When using strcpy () to assign a value to a character pointer space, the string size cannot exceed the space previously opened, otherwise the compilation can pass, but an error will occur during runtime
  3. You can directly assign a character pointer to another character pointer so that the two pointers point to the same memory area
  4. Use character pointer, character pointer plus &, character pointer plus & plus [0] to print out the complete string hello
  5. After using malloc to allocate memory, remember to free, and at the same time to determine the case of unsuccessful allocation 1

Array pointer as a function parameter

Look at the third piece of code

void dealArr(int num, int* pArr)
{
	int i = 0;
	for(i = 0; i < num; i++)
	{
		*(pArr+i) = (*(pArr+i))++;
	}
}

int a[5] = {0, 1, 2, 3, 4};
int j = 0;
dealArr(5, a);
for(j = 0; j < 5; j++)
{
	printf("%d,", a[j]);
}
  1. The defined function parameter type is int *, indicating a pointer to an integer
  2. You can directly pass in the array name a, or you can pass in the entire array & a, but it will prompt that the actual parameter types are not compatible, because the type of & a is int (*) [5], you can also pass in a [0], compile Will pass, but the operation will be wrong
  3. The pointer passed in the function can change the value pointed to by the address, the print result is 1, 2, 3, 4, 5,

  1. "C Traps and Defects" ↩︎

Published 173 original articles · Like 94 · Visit 210,000+

Guess you like

Origin blog.csdn.net/HEYIAMCOMING/article/details/105667874