C Question Index

I am really confused about how pointers work. I'm trying to write short, small programs that will accurately cover how they work, and I'm having some problems. For instance:

char c[3]; //Creates an array of 3 bytes - the first 2 bytes can be used for characters and the 3rd would need to be used for the terminating zero

*c = 'a'; //sets c[0] to 'a'
*c++; //moves the pointer to c[1]
*c = 'b'; //sets c[1] to 'b'
*c++; //moves the pointer to c[2]
*c = '\0' //sets c[2] to the terminating zero

Obviously, this code is incorrect, otherwise I would not poll the forum :)

I'm just having trouble understanding this from the book, can someone briefly explain the concept?

+3
source share
6 answers

, c , . , . , *c ( ) , c , , c , c++.

-, , *. , . , "", , . , (, ), , .

, , :

char c[3]; // Creates an array of 3 bytes - the first 2 bytes can be used for characters 
           // and the 3rd would need to be used for the terminating zero
char* p_c; // Creates a character pointer that we will use to refer into the c array

p_c = &c[0]; // Assign the address of the first element of the c array to the p_c pointer.
             // This could also be "p_c = c", taking advantage of the fact that in this
             // is one of the circumstances in which an array can be treated as if it were 
             // a pointer to its first element

*p_c = 'a'; //sets c[0] to 'a'
p_c++;      //moves the pointer to c[1] (note no *)
*p_c = 'b'; //sets c[1] to 'b'
p_c++;      //moves the pointer to c[2] (note no *)
*p_c = '\0' //sets c[2] to the terminating zero
+6

c , . , . ( rvalue).

++ . , :

char *d = c;
+15

- C. , (, ) . "", ( ).

...

void f( char* p );

char c_array [3];       // define an array
char *c_ptr = c_array;  // define a pointer and set it to point at the beginning of the array
                        // here array "decays" to pointer

*c_ptr = '1';
assert(c_array[0] == '1');
assert(c_ptr[0] == '1');   // this works too... in fact, operator [] is defined
                           // for pointers, not arrays, so in the line above array
                           // decays to pointer too.

++c_ptr;                   // move the pointer
//++c_array;               // -- this won't compile, you can't move the array

*c_ptr++ = '2';
*c_ptr   = '\0';
assert(c_array[1] == '2');
assert(c_array[2] == 0);

assert(sizeof(c_array) == 3);  // no decay here!

assert(sizeof(c_ptr) == sizeof(void*));  // a pointer is just a pointer

f(c_array);                // array-to-pointer decay, again


// now, what happens here?
void g( char param [100] )
{
    ++param;  // it works!
              // you can't pass an array as a parameter by value.
              // The size in the parameter declaration is ignored; it just a comment.
              // param is a pointer.

    assert(sizeof(param) == sizeof(void*));
              // yes, it just a pointer

    assert(*param == '2'); // in the call below
}


g(c_array);   // array-to-pointer decay, again

, .

( , . , C).

EDIT:

+3

. , . , , MIPS ...

, .

#include <stdio.h>

int main()
{
    char c_arr[3] = {'a', 'b', '\0'};       // Array of 3 chars.
    char* c_ptr = c_arr; // Now c_ptr contains the address of c_arr.

    // What does it mean that c_ptr "contains the address of c_arr"?
    // Underneath all this talk of "pointers" and "arrays", it all
    // just numbers stored in memory or registers. So right now, c_ptr is
    // just a number stored somewhere in your computer.

    printf("%p\n", c_ptr);
    // I got `0xbf94393d`. You'll get something different each time you run it.

    // That number (0xbf94393d) is a particular memory location. If you
    // want to use the contents of that memory location, you use the *
    // operator.
    char ch = *c_ptr;
    // Now ch holds the contents of whatever was in memory location 0xbf94393d.
    // You can print it.

    printf("%c\n", ch);
    // You should see `a`.

    // Let say you want to work with the next memory location. Since
    // the pointer is just a number, you can increment it with the ++ operator.
    c_ptr++;
    // Let print it to see what it contains.

    printf("%p\n", c_ptr);
    // I got 0xbf94393e. No surprises here, it just a number -- the
    // next memory location after what was printed above.

    // Again, if we want to work with the value we can use the *
    // operator. You can put this on the left side of an assignment
    // to modify the memory location.
    *c_ptr = 'z';

    // Since c_ptr was pointing to the middle of our array when we
    // performed that assignment, we can inspect the array to see
    // the change.
    printf("%c\n", c_arr[1]);

    // Again, c_ptr is just a number, so we can point it back to
    // where it was. You could use -- for this, but I'll show -=.
    c_ptr -= 1;

    // We can also move by more than one. This will make the pointer
    // contain the address of the last memory location in the array.
    c_ptr = c_ptr + 2;

    return 0;
}

. - . , .

++++++++++++++++++++++++++++++++++++++++++++
|  NAME   |   ADDRESS    |   VALUE         |
+=========+==============+=================+
|  c_arr  |  0xbf94393d  |   'a'           |
|         |  0xbf94393e  |   'b'           |
|         |  0xbf94393f  |   '\0'          |
+---------+--------------+-----------------+
|  c_ptr  +  <someaddr>  |   0xbf94393d    |
+------------------------------------------+

, , c_arr[0], ​​. , c_ptr . *c_ptr, CPU, 0xbf94393d . *c_ptr = 'z' ", 0xbf94393d " z "" - .

+1

, , . c++ .

+1

Try

c++;
*c = 'b';
c++;
*c = '\0';

The * operator is trying to dereference a pointer. All you have to do is move the pointer and then complete your task.

0
source

Source: https://habr.com/ru/post/1762543/


All Articles