Collate the features and usage of various types of Pointers in the C language

  • 2020-05-09 18:59:15
  • OfStack

Why Pointers distinguish between types:

In the same compiler environment, the memory space occupied by a pointer variable is fixed. For example, in a 16-bit compiler environment, any one pointer variable takes only 8 bytes and does not change depending on the type of the variable being pointed to.

Although all Pointers take up only 8 bytes, different types of variables take up different Numbers of bytes.

One int takes 4 bytes, one char takes 1 byte, and one double takes 8 bytes.

Now that I only have one address, how do I know how many bytes of memory to access backwards from that address, four, one, or eight.

So the pointer variable needs the data type it points to to tell it how many bytes of storage it wants to access.

That is, if you don't specify the type of pointer, then when the pointer points to a variable, it starts at the first address, but it doesn't know when to stop, and it doesn't know how many storage Spaces to access. So let's say I have a variable of type char, and I have a pointer to it, but I'm going to set the pointer to type int, so if I have a pointer of type char, then the pointer is going to go back to 4 bytes of storage space, and it's going to get a result that's obviously not what type char is supposed to get, so distinguish between types.

Only Pointers can be manipulated (moved), not array names.


 int x[10];
 x++; //illegal
 int* p = x;
 p++; //legal

The difference between two pointer variables is the number of elements in the array between the two Pointers.

It's really the difference between two pointer values (addresses) divided by the length of the array element (bytes).
(pointer2 address value - pointer address value)/sizeof(data type pointed to)
Pointers can be subtracted from one another, but not added (adding is meaningless).
Definition string:

Character array:


char string[] = "hello";
printf("%s\n",string);

String pointer to a string:


char *str = "hello"

The string stored in the character array is stored in the "stack", so it is readable and writable, so we can modify the value of an element in the character array.

However, you use a character pointer to hold the string, which holds the string constant address, and the "constant area" is read-only, so it is immutable.


char *str = "hello";
*(str+1) = 'w'; //  error 

Usage note:


char *str;
scanf("%s", str); 

/* str is 1 Pointer to a wild , He's not pointing to something 1 Block memory space , So that's not allowed. If you give str Allocation of memory space can be used in this way  */

 /*********  Array method ****************/ 

 char name[20]; 
 scanf("%s",name);  

/*************  A method of allocating memory space to a character pin ***********/ 

 char *name; 
 name=(char*)malloc(50);   // At this time name Have been to 1 The address space just allocated.  
 scanf("%s",name);

Pointer function (function, return value is pointer) note:

If the function returns 1 string, an error will be reported if the array is in the following form:


char *test() {
  return "hello";
}

int main(int argc, const char * argv[]) {

  char names[10];

  names = test();

  return 0;
}

This is because the returned string is equivalent to one such array: {' h', 'e',' l', 'l',' o', '\0'}, but as we said before, if the array is not initialized in the {} way when defined, then it cannot be initialized in this way any more, so it will fail.

Solution: change char names[10] to char *names or char names[10] directly to test().

Function pointer (pointer to a function) :

Format: return value type of the function (* pointer variable name) (parameter 1, parameter 2...) ;


int sum(int a,int b)
{
 return a + b;
}

int (*p)(int,int);
p = sum;

Application scenarios:

Call a function

Pass functions as arguments between functions

Function Pointers can be more flexible:


int minus(int a, int b) 
{
  return (a - b);
}

int add(int a, int b)
{
  return (a + b);
}

int myFunction(int a, int b, int (*funcP) (int, int))
{
  return funcP(a, b);
}

int main()
{
  int minusResult = myFunction(10, 20, minus);
  int addResult = myFunction(10, 20, add);
   ...
  return 0;
}

/*
   Function Pointers can make your program more flexible. For example, when you multiply or divide functions, you just need to implement the two functions and call them in the main function myFunction Function. If you have multiple people working together, different people writing different functions, if we write myFunction So basically you don't have to modify it 1 Direct use, very flexible. 
*/

Skills:

1. Copy the function head to point to

2. Enclose the function name in parentheses

Add a * to the function name

4. Modify the function name

Usage note:

Since these pointer variables store the entry address of a function, adding and subtracting them (such as p++) is meaningless.

In the example above, if you want to use the p function pointer, you can use sum1 directly:


int result = p(10, 10);

You can also do this:


int result = (*p)(10, 10);

A struct is a custom data type. Note that it is a data type.


char string[] = "hello";
printf("%s\n",string);
0

Notice that the back of the structure is there; .

When using a struct type, add the struct keyword.

Define variables while defining the structure type:


char string[] = "hello";
printf("%s\n",string);
1

This kind of definition also defines the variable at the same time, which is equivalent to:


char string[] = "hello";
printf("%s\n",string);
2

Define variables while defining the struct type. If you want to continue using the struct type, you can still define it in the usual way:


struct Student newStu;

The anonymous struct defines the struct variables:


char string[] = "hello";
printf("%s\n",string);
4

While this anonymous approach looks more concise (leaving out the structure name) than the one above, note that this only defines one stu variable, and no new variables can be defined because the structure name is gone.


Related articles: