A small question about the structure pointer variable for C

Updated on technology 2024-05-19
17 answers
  1. Anonymous users2024-02-11

    The reason for this problem is that there is a problem with the create function of the linked list, and the problem is located in the red box in the figure.

    Each node here uses a local variable, after the end of the function, although the content of the local stack will not be cleared, but the memory will be **, if the stack is also used elsewhere may be allocated to it, the memory content will change. The correct way to do this is to request heap memory. Use the malloc function and include the header file include.

    Replace the ** in the box with :

    struct student *b = struct student*)malloc(sizeof(struct student));

    b->next = null;

    b->num = 2;

    struct student *a = struct student*)malloc(sizeof(struct student));

    a->next = b;

    a->num = 1;

    struct student *head=a;

  2. Anonymous users2024-02-10

    The pointers and arrays in C are interesting.

    For example, if you define an array a[10], you can access the elements in it via a[1], or you can access it with *(a 1).

    Why is it used this way?

    The type of ptr is (int *), so what ptr stores in memory is a pointer of type int, pointing to an int address. Under normal circumstances, you can access the data in a certain address it points to through PTR, as you can see in the following figure 22.

    There is a malloc function in your program here, and its function is to apply for space, which originally had only one space, and there can be a lot of continuous space after application. At this time, if the value in the ptr 0000001 plus 1 points to the space below 22, you can continue to store things, as shown in the figure.

    That's where the pointer comes in, it has direct control over the address space. ptr[i] means the same as *(ptr i).

    So when scanf, you can put something directly to the address of ptr[i].

    Similarly, char *p can also be turned into an array, as long as you give it space. This is used a lot in data structures and algorithms, and it's also very interesting, but it's easy to get dizzy.

  3. Anonymous users2024-02-09

    Definition of a pointer variable.

    Pointer variables have three properties:

    1) The type of variable to which the pointer variable points to. For example, the variable i pointed to by the i pointer is an integer.

    2) How many memory units the pointer variable occupies in memory. For example, if i pointer occupies two memory units, it is called a "near pointer", which is represented by near. If the variable occupies 4 memory units in memory, it is called a "far pointer" and is denoted by far.

    If no NEAR or FAR is specified, NEAR is the default. (Pointer variables take up either 2 or 4 memory units in memory).

    3) Which variable the pointer variable points to, that is, what is the value of the pointer variable. For example, the value of i pointer is 2000.

    The general form of the pointer variable definition:

    Type Identifier * Identifier.

    " indicates that a pointer variable is defined.

    "Identifier" is the name of the pointer variable.

    Type Identifier indicates the type of variable to which the pointer variable points to.

    Example, int i, j; Define two integer variables

    int *pointer_1, *pointer_2;

    float *pointer_3;

    char *pointer_4;

    void *pointer_5;

    char far *pointer_6;

    Assignment of pointer variables: Example,

    pointer_1 = &i;

    pointer_2 = &j;

    pointer_1 = 100;

    You can also specify the initial value of a pointer variable at the same time as defining it, such as

    int a;

    int *p = &a;

  4. Anonymous users2024-02-08

    int a;

    a=20;int *pa=&a;

    printf("%d",*pa);

    This approach is wrong! The reason is that variables in C are used first and later, and variables in C are defined at the beginning of the statement, but in C++, they can be defined as they go.

    **mr computer

  5. Anonymous users2024-02-07

    No, I tried it with VS2005 and both methods worked, no mistake.

  6. Anonymous users2024-02-06

    I didn't find any errors, can I post all your codes?

  7. Anonymous users2024-02-05

    1. The concept is different.

    "Pointer" is a concept, "pointer variable" is a concrete implementation, and a pointer is also a variable, so it needs to be defined, and the definition of a pointer is the same as a general variable.

    2. The storage address is different.

    The (memory) address of a variable is called the "pointer" to the variable, and the memory unit with its address can be found by the pointer. A pointer variable is used to store the address of another variable (i.e., a pointer).

    The relationship between pointers and pointer variables.

    1. The pointer is the address, and the address is the pointer.

    2. The address is the number of the memory unit.

    3. The pointer variable is the variable that stores the memory address.

    4. Pointer and pointer variable are two different concepts, but it should be noted that usually we will refer to pointer variables as pointers when we describe them, but they actually have different meanings.

    Benefits of pointers:

    1. Direct access to hardware.

    2. Quickly transfer data (pointer indicates address).

    3. Return more than one value and return one (pointer to array or struct) 4. Represent complex data structures (structs).

    5. It is convenient to process strings.

    6. Pointers help to understand object orientation.

  8. Anonymous users2024-02-04

    The more strict way to put it is this:

    The system assigns an address value to each memory cell, which C++ calls a "pointer". If there is int i=5; , the number (address) of the memory cell where the variable i is stored & i is called the pointer.

    The "pointer variable" is the variable that stores the "address value" mentioned above, or it can be expressed as the variable that stores the "first address" of the memory space occupied by the variable (because a variable usually occupies multiple bytes of space in a row). For example, in int i=5; There is a sentence int *p=&i;, the pointer &i of i is assigned to the int * pointer variable p, which means that &i is stored in p. So the pointer variable is the variable that holds the pointer.

    It is worth noting the fact that there are many sources and textbooks that do not make the above distinction, but rather consider "pointers to be short for pointer variables", such as int *p=&i;The explanation is: declare an int * pointer p and initialize it with the address of variable i; Strictly speaking, you should declare an int * pointer variable p. Therefore, sometimes it is necessary to understand the essence of the book according to the context, and not to be too rigid in the textual expression.

  9. Anonymous users2024-02-03

    A pointer in C++ is an address that points to a data storage address, such as an int

    a;intp=&a, which refers to the address of variable a; The pointer variable is to store the address where the data is stored in a variable, first find the address in the variable, and then find the data from this address, so it is basically the same.

  10. Anonymous users2024-02-02

    The definition of a null pointer is slightly different.

    In C: define

    nullvoid*)0

    In C++: Define

    null0 and the number 0 are the only values that can be assigned directly to the pointer.

    In addition, types such as class members, pointers, etc., are not available in C++.

  11. Anonymous users2024-02-01

    A pointer is an address, and a pointer variable is a variable that stores an address.

    The basic concept of pointers.

    In a computer, all data is stored in memory.

    Generally, a byte in memory is called a memory unit, and the number of memory cells occupied by different data types varies, such as 2 units for integers and 1 unit for characters. In order to properly access these memory cells, each memory cell must be numbered.

    A memory cell can be accurately located by its number. The number of a memory cell is also called an address.

    Since you can find the memory cell you need by its number or address, it is common to refer to this address as a pointer.

    For a memory cell, the address of the cell is the pointer, and the data stored in it is the content of the cell. In C, it is allowed to store pointers in a variable, which is called a pointer variable. Thus, the value of a pointer variable is the address of a memory cell, or a pointer called a memory cell.

  12. Anonymous users2024-01-31

    The (memory) address of a variable is called the "pointer" to the variable, and the memory unit with its address can be found by the pointer. A pointer variable is used to store the address of another variable (i.e., a pointer).

  13. Anonymous users2024-01-30

    In fact, it is the same as the difference between "integer" and "integer variable".

    In simple terms, a "pointer" is an address, and a "pointer variable" is the value of an address.

  14. Anonymous users2024-01-29

    The concept of pointer is one of the important elements of C C++, which is a type of variable that stores the address of the specified type of data, while a variable of the same type stores the data.

    A pointer variable, as the name suggests, is a variable whose value is mutable, exactly the same as the naming conventions for integer variables, floating-point variables, and so on.

    "Pointer" is the concept, and "Pointer Variable" is the concrete implementation.

  15. Anonymous users2024-01-28

    First of all, you have to understand what a pointer is, a pointer is the address of a piece of memory, and the pointer variable is used to store this address, that is, the pointer variable is stored in the pointer, just like the integer variable is stored in the integer.

  16. Anonymous users2024-01-27

    A pointer variable is a variable that stores an address, usually after defining variables such as integer and solid, the system allocates a certain amount of memory to the variable to store data, and the space allocated to it by defining a pointer variable is used to store the address. And only the address can be stored.

  17. Anonymous users2024-01-26

    Pointers are the essence of the C language, and by using pointers, we can make good use of memory resources and make them as efficient as possible. With pointer technology, we can describe complex data structures, the processing of strings can be more flexible, the processing of arrays can be more convenient, and the writing of programs is concise, efficient and refreshing. But because the fingers are for beginners, it is difficult to understand and grasp.

    Mastering requires a certain amount of knowledge of computer hardware as a foundation, which requires more practice and more hands-on work, in order to master it as soon as possible in practice and become a master of C.

    In the past, when we defined or described variables in programming, the compilation system allocated corresponding memory units to the defined variables, that is, each variable had a fixed location in memory and a specific address. Due to the different data types of variables, the number of memory units it occupies is also different. If we define it in the program:

    int a=1,b=2;

    float x=, y = 4 . 5 ;

    double m=;

    char ch1='a', ch2='b';

    Let's take a look at how the build system allocates memory for variables. Variables a and b are integer variables, each accounting for 2 bytes in memory; x and y are real types, each accounting for 4 bytes; m is a double-precision real type, which occupies 8 bytes; ch1 and ch2 are character-based, each accounting for one byte. Since the computer memory is addressed by bytes, and the storage of variables starts from 2000 units of memory, the placement of variables in memory by the compilation system is shown in Figure 6-1.

    For example, the address of variable A in memory is 2000, and after occupying two bytes, the memory address of variable B is 2002, and the memory address of variable M is 2012. Access to in-memory variables, used to be used with scanf("%d%d%f", &a, &b, &x) indicates the memory unit indicated by the address of the variable that will input the data. Then, to access a variable, you should first find its address in memory, or in other words, an address that uniquely points to a memory variable, which we call an address.

    A pointer to a variable. If the addresses of variables are stored in a specific area of memory, and the addresses are stored in variables, such variables are pointer variables, which are a kind of "indirect access" to the variable by pointing to the access to the variable it points to.

    A set of pointer variables pa, pb, px, py, pm, pch1, and pch2 point to the above variables a, b, x, y, m, ch1, and ch2, respectively, and the pointer variables are also stored in memory, as shown in Figure 6-2

    In Figure 6-2, the memory shown on the left stores the value of the pointer variable, which gives the address of the referring variable, through which the variable described on the right can be accessed. For example, if the value of the pointer variable pa is 2000, it is the address of variable a in memory. Therefore, pa points to variable a.

    The address of the variable is the pointer, and the variable that holds the pointer is the pointer variable.

Related questions
9 answers2024-05-19

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

6 answers2024-05-19

The p of both is a pointer.

p=&t, change the content that p points to to the address of t, and p=&t is the address that changes the pointer to t. >>>More

4 answers2024-05-19

I've seen questions like this, and I don't understand what you mean, but I think it's like this, structab; I hit ... where you can add definitions of all data forms, e.g., int >>>More

6 answers2024-05-19

It seems that you don't know much about structs and struct pointers, the data array is a struct array that you define, it consists of two knot body elements, and each struct element contains two members x and y, the first element is 1 and 10, the second element is 2 and 20, the struct pointer p you defined starts to point to the first element of the array data, p points to the second element of the data array, and the pointer p can manipulate the two members x and y >>>More

19 answers2024-05-19

Provision for bad debts accrued at the end of 2006: 1,200,000 * 3% = 36,000 >>>More