Difference between using character pointers and character arrays

前端 未结 9 791
不思量自难忘°
不思量自难忘° 2020-11-29 03:45

Basic question.

char new_str[]=\"\";

char * newstr;

If I have to concatenate some data into it or use string functions like strcat/substr/

相关标签:
9条回答
  • 2020-11-29 04:01

    This is a character array:

    char  buf [1000];
    

    So, for example, this makes no sense:

    buf = &some_other_buf;
    

    This is because buf, though it has characteristics of type pointer, it is already pointing to the only place that makes sense for it.

    char *ptr;
    

    On the other hand, ptr is only a pointer, and may point somewhere. Most often, it's something like this:

    ptr = buf;              // #1:  point to the beginning of buf, same as &buf[0]
    

    or maybe this:

    ptr = malloc (1000);    // #2:  allocate heap and point to it
    

    or:

    ptr = "abcdefghijklmn"; // #3:  string constant
    

    For all of these, *ptr can be written to—except the third case where some compiling environment define string constants to be unwritable.

    *ptr++ = 'h';          // writes into #1: buf[0], #2: first byte of heap, or
                           //             #3 overwrites "a"
    strcpy (ptr, "ello");  // finishes writing hello and adds a NUL
    
    0 讨论(0)
  • 2020-11-29 04:04

    If you're in c++ why not use std::string for all your string needs? Especially anything dealing with concatenation. This will save you from a lot of problems.

    0 讨论(0)
  • 2020-11-29 04:06

    If you're using C++ as your tags indicate, you really should be using the C++ strings, not the C char arrays.

    The string type makes manipulating strings a lot easier.

    If you're stuck with char arrays for some reason, the line:

    char new_str[] = "";
    

    allocates 1 byte of space and puts a null terminator character into it. It's subtly different from:

    char *new_str = "";
    

    since that may give you a reference to non-writable memory. The statement:

    char *new_str;
    

    on its own gives you a pointer but nothing that it points to. It can also have a random value if it's local to a function.

    What people tend to do (in C rather than C++) is to do something like:

    char *new_str = malloc (100); // (remember that this has to be freed) or
    char new_str[100];
    

    to get enough space.

    If you use the str... functions, you're basically responsible for ensuring that you have enough space in the char array, lest you get all sorts of weird and wonderful practice at debugging code. If you use real C++ strings, a lot of the grunt work is done for you.

    0 讨论(0)
  • 2020-11-29 04:09

    The excellent source to clear up the confusion is Peter Van der Linden, Expert C Programming, Deep C secrets - that arrays and pointers are not the same is how they are addressed in memory.

    With an array,

    char new_str[];
    the compiler has given the new_str a memory address that is known at both compilation and runtime, e.g. 0x1234, hence the indexing of the new_str is simple by using []. For example new_str[4], at runtime, the code picks the address of where new_str resides in, e.g. 0x1234 (that is the address in physical memory). by adding the index specifier [4] to it, 0x1234 + 0x4, the value can then be retrieved.

    Whereas, with a pointer, the compiler gives the symbol

    char *newstr
    an address e.g. 0x9876, but at runtime, that address used, is an indirect addressing scheme. Supposing that newstr was malloc'd
    newstr = malloc(10);
    , what is happening is that, everytime a reference in the code is made to use newstr, since the address of newstr is known by the compiler i.e. 0x9876, but what is newstr pointing to is variable. At runtime, the code fetches data from physical memory 0x9876 (i.e. newstr), but at that address is, another memory address (since we malloc'd it), e.g 0x8765 it is here, the code fetches the data from that memory address that malloc assigned to newstr, i.e. 0x8765.

    The char new_str[] and char *newstr are used interchangeably, since an zeroth element index of the array decays into a pointer and that explains why you could newstr[5] or *(newstr + 5) Notice how the pointer expression is used even though we have declared char *newstr, hence

    *(new_str + 1) = *newstr;
    OR
    *(new_str + 1) = newstr[1];

    In summary, the real difference between the two is how they are accessed in memory.

    Get the book and read it and live it and breathe it. Its a brilliant book! :)

    0 讨论(0)
  • 2020-11-29 04:09

    To differentiate them in the memory allocation side:

    // With char array, "hello" is allocated on stack
    char s[] = "hello";
    
    // With char pointer, "hello" is stored in the read-only data segment in C++'s memory layout.
    char *s = "hello";
    
    // To allocate a string on heap, malloc 6 bytes, due to a NUL byte in the end
    char *s = malloc(6);
    s = "hello";
    
    0 讨论(0)
  • 2020-11-29 04:11

    The type of the first is char[1], the second is char *. Different types.

    Allocate memory for the latter with malloc in C, or new in C++.

    char foo[] = "Bar";  // Allocates 4 bytes and fills them with
                         // 'B', 'a', 'r', '\0'.
    

    The size here is implied from the initializer string.

    The contents of foo are mutable. You can change foo[i] for example where i = 0..3.

    OTOH if you do:

    char *foo = "Bar";
    

    The compiler now allocates a static string "Bar" in readonly memory and cannot be modified.

    foo[i] = 'X';  // is now undefined.
    
    0 讨论(0)
提交回复
热议问题