简体   繁体   English

C数组指针分割错误

[英]C array pointer segmentation fault

I'm not familiar to pointers and I stumbled upon segmentation fault on my code, wheres if I don't use pointers this code runs perfectly. 我对指针不熟悉,并且偶然发现了代码上的段错误,如果我不使用指针,则该代码可以完美运行。

#include <stdio.h>
#include <string.h>

int main()
{ 

    char *string[100],i,j;
    char *(*odd)[100];
    i = j = 0;

    fgets(*string, 100, stdin);

    while (*string[i] != '\0') {
        if (i % 2 == 0) {
                *odd[j++] = string[i];
        }
        i++;
    }

    *odd[j] = '\0';

    printf("Characters at odd position: %s\n",*odd[j]);

    return 0;
}

I'm guessing that I'm printing the odd array the wrong way, but I can't print it just using *odd as well. 我猜想我以错误的方式打印了odd数组,但是我也不能只使用*odd来打印它。

char *string[100],i,j;
fgets(*string, 100, stdin);

You are trying to write to address at string[0] but it is not initialized. 您正在尝试写入string[0]处的地址,但未初始化。 I guess you mean smth like 我想你是说像

char string[100];
fgets(string, 100, stdin);

PS char *string[] is an array of pointers to char while char string[] is an array of chars, and for this case in expressions string decays to pointer to first element ie to pointer to char. PS char *string[]是char的指针数组,而char string[]char string[]的数组,在这种情况下,表达式中的string衰减为指向第一个元素的指针,即指向char的指针。

With pointer: 带指针:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define SIZEOF_BUFFER 100

int main(){ 
    char *string; // string is a pointer
    char i,j;

    char *odd; // odd is a pointer

    // allocate memory to be able to store data
    string = malloc(sizeof(char) * SIZEOF_BUFFER);

    if (string == NULL) {
        exit(-1);
    }

    odd = malloc(sizeof(char) * SIZEOF_BUFFER);

    if (od == NULL) {
        free(string);
        exit(-1);
    }

    i = j = 0;

    fgets(string, SIZEOF_BUFFER, stdin);

    while (string[i] != '\0') {
        if (i % 2 == 0) {
                odd[j++] = string[i];
        }
        i++;
    }

    odd[j] = '\0';

    printf("Characters at odd position: %s\n",odd);

    // Don't forget to free allocated memory
    free(odd);
    free(string);

    return 0;
}

Without pointer: 没有指针:

#include <stdio.h>
#include <string.h>

int main(){ 
    char string[100],i,j;

    char odd[100];

    i = j = 0;

    fgets(string, 100, stdin);

    while (string[i] != '\0') {
        if (i % 2 == 0) {
                odd[j++] = string[i];
        }
        i++;
    }

    odd[j] = '\0';

    printf("Characters at odd position: %s\n",odd);

    return 0;
}

also, no need of *string[strlen(*string) - 1] = '\\0'; 另外,不需要*string[strlen(*string) - 1] = '\\0'; => fgets put always a null character at the end of string => fgets在字符串末尾始终放置一个空字符

This is a problem with precedence of [] vs *. 这是[] vs *优先级的问题。 See http://en.cppreference.com/w/c/language/operator_precedence . 请参阅http://en.cppreference.com/w/c/language/operator_precedence To solve it do the dereferences like this: 要解决此问题,请执行以下取消引用:

(*odd)[j] = '\0';

This also applies to declarations; 这也适用于声明; to declare string as a pointer to an array of 100 chars, change it to: 将字符串声明为指向100个字符的数组的指针,请将其更改为:

char (*string)[100];

You also must allocate memory. 您还必须分配内存。 Here is a changed version of your code with all these changes: 这是所有这些更改的代码更改版本:

#include <stdio.h>
#include <string.h>

int main(){
    char actualstring[100];
    char (*string)[100] = &actualstring, i = 0, j = 0;
    char actualodd[100];
    char (*odd)[100] = &actualodd;

    fgets(*string, 100, stdin);

    (*string)[strlen(*string) - 1] = '\0';

    while ((*string)[i] != '\0') {
        if (i % 2 == 0) {
                (*odd)[j++] = (*string)[i];
        }
        i++;
    }

    (*odd)[j] = '\0';

    printf("Characters at odd position: %s\n",*odd);

    return 0;
} 

You may find it useful to translate declarations to english and vice-versa at http://cdecl.org/ http://cdecl.org/上,您可能会发现将声明翻译成英语(反之亦然)很有用。

string is an array of pointers. string是一个指针数组。 You did not initialize any of those pointers. 您没有初始化任何这些指针。 But then you write through those pointers as if they pointed somewhere. 但是随后,您将这些指针指向它们,就像它们指向某处一样。

Your code is a more complicated version of: 您的代码是以下版本的更复杂版本:

char *ptr;
fgets(ptr, 100, stdin);

in which (hopefully) it should be obvious what the problem is. 在哪里(希望)应该清楚是什么问题。 odd has a similar problem. odd也有类似的问题。

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

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