简体   繁体   English

C、char** 奇怪的输出

[英]C, char** strange output

#include    <stdio.h>

char**      StringArray ( int n_size )
{
    char*   astr_allocate[ n_size ];
    char**  pstr_string_array = astr_allocate;
    int n_count;

    for ( n_count = 0; n_count < n_size; n_count++ )
        *(pstr_string_array + n_count) = " ";

    *(pstr_string_array + n_size) = "\0";

    return  pstr_string_array;
}


char*       String      ( int n_size )
{
    char    ach_allocate[ n_size ];
    char*   str_string = ach_allocate;
    int n_count;

    for ( n_count = 0; n_count < n_size; n_count++ )
        *(str_string + n_count) = ' ';

    *(str_string + n_size) = '\0';

    return  str_string;
}


void main ()
{

    int n_size      = 5;
    int n_count     ;
    char*   pch_string  = String ( n_size );
    char**  pstr_string = StringArray ( n_size );

    for ( n_count = 0; n_count < n_size; n_count++ )
        printf  ( "%c", *(pch_string + n_count) );

    for ( n_count = 0; n_count < n_size; n_count++ )
        printf  ( "%s", *(pstr_string + n_count) );

    printf  ( "\n\n" );
}

This produces wonderful outputs of "???"这产生了“???”的精彩输出(Literal question marks) and random stuff like that. (字面问号)和诸如此类的随机内容。 I am just trying to understand pointers and string type stuff more, if someone could help out that would be great thankyou!我只是想更多地了解指针和字符串类型的东西,如果有人能帮忙,那就太好了,谢谢!

additionally: Been writing and compiling this in a linux terminal and nano, if that changes anything另外:在Linux终端和nano中编写和编译它,如果有任何改变

I would recommend that you study arrays, pointers and strings more.我建议你多研究数组、指针和字符串。

  • Since this program is a program for a regular hosted system (Linux), you must declare main as int main (void) .由于此程序是用于常规托管系统 (Linux) 的程序,因此您必须将 main 声明为int main (void)
  • C does not have a pre-made string class, C strings are arrays of characters. C 没有预制的字符串类,C 字符串是字符数组。 As such they need to actually be allocated somewhere.因此,它们实际上需要分配到某个地方。 You do try to allocate them as local variables inside a function, but you cannot return a pointer to a local variable, as that variable will cease to be valid once you leave the function.您确实尝试将它们分配为函数内的局部变量,但不能返回指向局部变量的指针,因为一旦离开函数,该变量将不再有效。 The memory which was previously reserved for your array may now be used for completely unrelated things at any time.以前为您的阵列保留的内存现在可以随时用于完全不相关的事情。 See this . 看到这个
  • Arrays in C are zero-indexed. C 中的数组是零索引的。 Meaning that given an array int arr[2];意思是给定一个数组int arr[2]; the items have index [0] and [1].项目具有索引 [0] 和 [1]。 Therefore you can't access it as arr[2] = ... or *(arr + 2) = ... , because that points past the end of the array.因此,您不能以arr[2] = ...*(arr + 2) = ...访问它,因为它指向数组末尾。
  • Given a pointer to character, which points at an array of characters (C string), you can't assign data to it by doing things like *(pstr_string_array + n_count) = " ";给定一个指向字符的指针,它指向一个字符数组(C 字符串),您不能通过执行诸如*(pstr_string_array + n_count) = " ";类的操作来为其分配数据*(pstr_string_array + n_count) = " "; because that will only change where the pointer points at!因为那只会改变指针指向的位置! You copy nothing.你什么都不复制。 Instead, you must use strcpy(pointer, " ", 2) .相反,您必须使用strcpy(pointer, " ", 2)
  • A string literal " " is the same as an array of characters char arr [2] = {' ', '\\0'} .字符串文字" "与字符数组char arr [2] = {' ', '\\0'} So code like "\\0" doesn't make any sense.所以像"\\0"这样的代码没有任何意义。
  • Pointers to pointers are not arrays, nor do they point to arrays, nor are they compatible with arrays.指向指针的指针不是数组,也不指向数组,也不兼容数组。 There is a lot of incorrect books and tutorials out there teaching blatantly incorrect uses of pointer-to-pointer.很多不正确的书籍和教程教人公然错误地使用指针到指针。 There's actually very few cases where you need to use it, 2D arrays is not one of them, so just forget you ever saw pointer-to-pointer for now.实际上,您需要使用它的情况很少,二维数组不是其中之一,所以暂时忘记您曾经见过指针到指针。
  • Please note that *(pointer + n) is 100% equivalent to pointer[n] .请注意*(pointer + n) 100% 等价于pointer[n] The latter is easier to read, so use that form when possible.后者更易于阅读,因此请尽可能使用该形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM