简体   繁体   English

使用指针修改字符串

[英]Using Pointers to Modify Strings

so I have a function that takes a Pointer to an array of 'Strings' (I understand strings as just blocks of memory that is followed by '\\0'). 因此,我有一个将指针指向“字符串”数组的函数(我将字符串理解为只是存储块,后跟“ \\ 0”)。 Since a string is already a pointer to the first byte of a string, my pointer is actually a ** doublePointer. 由于字符串已经是指向字符串第一个字节的指针,因此我的指针实际上是** doublePointer。 However I am seg-faulting off the Ying Yang and I honestly dont know what is going on in the low level view. 但是,我正在隔离盈阳,老实说,我不知道在低级别视图中发生了什么。 Here is my code below, its function is to read characters and capitalize the first letter of the first word (in string) and after a period. 这是我的下面代码,它的功能是读取字符并在一个句点之后大写第一个单词的首字母(字符串)。

    void autocaps(char ** words)
    {

    /* Add code here */
    //Period Boolean
    bool next=false;
    //First Word Boolean
    bool fcap=true;
    //Counter Variable
    int i=0;
    int j=0;
    //Second Pointer
    char** wordx = words;

    //LowerCase Bomb & Period Flagging
    while(wordx[i][j]!='\0'){
      while(wordx[i][j]!='\0'){
       //A-Z Filter
       if((wordx[i][j]>='A')&&(wordx[i][j]<='Z')){
      wordx[i][j]+=32;
       }
       if(wordx[i][j]=='.'){
      next=true;
       }
      j++;
      }
    i++;
    }

 i=0;
 j=0;
 //Cap First Word & Cap Post Period
 while(words[i]!='\0'){
   while(words[i][j]!='\0'){
    //a-z Filter
    if((words[i][j]>=97)&&(words[i][j]<=122)){
  if(fcap){
    words[i][j]-=32;
    fcap=false;
  }
  if(next){
    words[i][j]-=32;
  }
    }
    j++;
  }
 i++;
}
return;

} }

I am seg-faulting when I am printing the original pointer that was passed through the parameter. 当我打印通过参数传递的原始指针时,出现段错误。 If someone could explain to me the low level concept of this because I am so confused I am throwing triple and quadruple stars in everywhere and I dont even know if it brings me closer or farther from debugging my code. 如果有人因为我很困惑而向我解释这个低层次的概念,那么我到处都是三颗和四颗星星,我什至不知道它是否使我与调试我的代码更接近或更远。

Thank You!! 谢谢!!

You said 'array of strings' but based on your code and your intent (capitalizing first character and after periods) I am going to make the assumption you intend this to work on the single string you pass to the function. 您说的是“字符串数组”,但基于您的代码和意图(大写的第一个字符和句点后的大写字母),我将假设您打算将其用于传递给函数的单个字符串。

A string is just an array of characters followed by a 0 value. 字符串只是一个字符数组,后跟一个0值。 What you want to do is traverse the array of characters. 您要做的是遍历字符数组。 To get from a character pointer to a character you want to dereference the pointer. 要从字符指针到字符,您要取消对指针的引用。 That is why you see '*words' below everywhere we actually inspect the character we are at in the string. 这就是为什么您在实际上检查字符串中的字符的任何地方都看到“ * words”的原因。

Double, triple and quadruple pointers were taking you farther from the solution. 双,三和四重指针使您离解决方案更远。

Here is a reworked sample: 这是一个重做的示例:

void autocaps(char* words)
{
  bool fcap=true; /* true so first letter is made cap */

  while (*words != '\0') 
  {
    if (fcap)
    {
        /* capitalize */
        if ((*words >= 'a') && (*words <= 'z'))
        {
            *words -= 32;
        }

        fcap = false;
    }
    else
    {
        /* not cap */
        if ((*words >= 'A') && (*words <= 'Z'))
        {
            *words += 32;
        }
    }

    /* period - cap next letter */
    if (*words == '.')
    {
        fcap = true;
    }

    /* step to next character in array */
    words++;
  }

  return;
}

I you are initializing your array of strings as contant strings, something like: 我正在将字符串数组初始化为常量字符串,例如:

char *words[2] = {"hello", "world"};

then you will get a segmentation fault the first time you get to 那么您第一次访问时会遇到细分错误

**words+=32;

because you are attempting to modify read-only location. 因为您正在尝试修改只读位置。

You should initializing the strings like so: 您应该像这样初始化字符串:

char word1[] = {'h', 'e', 'l', 'l', 'o', 0};
char word2[] = {'w', 'o', 'r', 'l', 'd', 0};
char *words[] = {word1, word2};

The other problem is indeed that of 另一个问题确实是

*words++;

You do not want to do this for two reasons: 您不想这样做有两个原因:

  1. as other posters mentioned, ++ has precedence over the dereferencing operator, and so you are actually incrementing the words array from one string to the next, and not one character to the next. 正如其他张贴者所提到的,++优先于解引用运算符,因此实际上您是将word数组从一个字符串递增到下一个字符串,而不是将一个字符递增到下一个字符串。
  2. the dereferencing operator then does not have any affect. 这样,取消引用运算符就不会有任何影响。

Now, if you want to advance to the next character you do not want to do this: 现在,如果您想前进到下一个字符,则不需要这样做:

(*words)++

because now you are actually changing the pointer in the words array, and you do not want to do that. 因为现在您实际上正在更改words数组中的指针,并且您不想这样做。

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

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