#include <stdio.h>
int main ()
{
FILE *FP;
if ((Fp=fopen ("C://my/test.txt", "r+")) ==null) ()
{
printf ("The file is not open correctly and cannot be executed down .") \ n ");
return (1);
}
}
File-type pointers
#include <stdio.h>
int main ()
{
Int*ptr; declares an int pointer
Intval = 1; declares an int value
Ptr= &val; Assigning a reference to an int value for a pointer
Intderef = *ptr; value the pointer to print the contents stored in the pointer address
printf ("Deref address =%ld, value =%d\n", PTR, deref);
}
Line 2 , we declare an int pointer through the * operator . Then we declare an int variable and assign a value of 1. We then initialize our int pointer with the address of the int variable . Next, the int pointer is evaluated, and the int pointer is initialized with the memory address of the variable . Finally, we print out the output variable value, the content is 1.
the &val of line 6 is a reference. after the Val variable declares and initializes the memory, by using the address operator before the variable name & We can directly reference the memory address of the variable.
Line 8 , we once again use the * operator to value the pointer, you can directly get the data in the memory address pointed to by the pointer. Since the type of the pointer declaration is int, the value taken is the int value stored by the memory address pointed to by the pointer .
Pointers and Arrays
#include <stdio.h>
int main ()
{
Intmyarray[4] = {1,2,3,0};
int *ptr = myarray;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
ptr++;
printf ("PTR address =%ld, value *ptr=%d\n", ptr,*ptr);
}
the C-language array represents a contiguous memory space used to store multiple objects of a specific type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not of the same structure and therefore cannot be converted to each other. The array variable points to the memory address of the first element of the array.
An array variable is a constant. You cannot assign a pointer to an array variable, even if the pointer variable points to the same address or to a different array. It is also not possible to assign an array variable to another array. However, it can be confusing to assign an array variable to the pointer. When assigning an array variable to a pointer, it is actually assigning the address to the first element of the array to the pointer.
Pointers and structures
#include <stdio.h>
struct Person {
Intage
Char*name;
};
int main ()
{
struct person first;
struct person *ptr;
First.age = 21;
Char*fullname = "full name";
First.name = fullname;
Ptr= &first;
printf ("age=%d, name=%s\n", First.age, Ptr->name);
}
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
Main ()
{
Intcount,*array; /*count is a counter,array is an integer pointer, or it can be interpreted as pointing to the first address of an integer array */
if ((array= (int *) malloc (10*sizeof (int))) ==null)
{
printf ("The storage space cannot be allocated successfully.") ");
Exit (1);
}
for (count=0;count<10;count++)/* assign a value to an array */
Array[count]=count;
for (count=0;count<10;count++)/* Print array elements */
printf ("%d-", Array[count]);
}
in the example above , a total of ten integer storage areas are allocated dynamically and then assigned and printed. The if ((array= (int)) malloc (10*sizeof (int))) ==null) statement in the example can be divided into the following steps:
1) Allocate a contiguous storage space of ten integers and return an integer pointer to its start address
2) Assign this integer pointer address to the array
3) detects if the return value is NULL
2.Free function
because the area of memory is always limited, it cannot be allocated indefinitely, and a program should try to conserve resources so that when the allocated memory area is not used, it should be released for other variables or programs to use. At this point we need to use the free function.
Its function prototypes are:
void free (void *p)
The function is to release the memory area that the pointer p points to.
its parameter p must be a pointer that was returned when the malloc function or the Calloc function was previously called (another function that dynamically allocates the storage area). passing other values to the free function is likely to result in a crash or other catastrophic consequence.
Note: What is important here is the value of the pointer, not the pointer itself that is used to request dynamic memory. Cases:
int *p1,*p2;
P1=malloc (10*sizeof (int));
P2=P1;
......
Free (p1)/* or free (p2) */
The malloc return value is assigned to P1, and the value of P1 is assigned to P2, so P1,p2 can be used as arguments to the free functionat this time .
The malloc function allocates a storage area.
The free function frees an area of memory that is already unused.
malloc function
the prototype of the malloc function is:
void *malloc (unsigned int size)
The function is to allocate a continuous space of size in the dynamic storage area of memory . Its argument is an unsigned number, and the return value is a pointer to the starting address of the allocated contiguous storage domain. It is also important to note that a NULL pointer is returned when the function fails to allocate storage space (such as low memory) successfully . Therefore, the function should be called to detect if the return value is NULL and perform the appropriate operation.
Third C language Course design assignments