1

用户输入一个词。然后我的代码找到单词的长度,然后通过设置指向单词开头和结尾的指针来比较单词的字母。它开始增加“开始指针”并减少“结束指针”并比较循环中的字母。我的问题是如何在单词的开头和结尾分配指向字母的指针???我尝试为这个词分配一个指针。在 printf 之后它给了我一个数字……我认为它是单词或 smth 的地址……这是我的代码……

#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[]){
    int count,i;
    char *beginner_pointer;
    char *ending_pointer;
    //printf("This program was called with \"%s\".\n",argv[0]);
    if (argc > 1){

        for (count=1; count<argc; count++){    
      //      printf("argv[%d]=%s\n", count, argv[count]);
            int length = strlen(argv[count]);
           beginner_pointer = argv[count];
            printf("%d\n", *beginner_pointer);
        }    

    }
    //int length = strlen(argv[1]);
    //printf("%d", length);

    return 0;
}
4

2 回答 2

2
beginner_pointer = argv[count];
ending_pointer = beginner_pointer + length - 1;
于 2013-10-29T21:49:22.120 回答
1
#include <stdio.h>
#include <string.h>   // required for strlen()

int main(void) {

    char *p1, *p2;    // declare two char pointer

    char *s = "EVITATIVE";  // this is our string

    int sl = strlen(s);   // returns the length of the string, requires string.h

    p1 = s;             // p1 points to the first letter of s
    p2 = s+sl-1;        // p2 points to the last letter of s, -1 because of \0

    printf("%c\n", *p1);   // prints the first letter

    printf("%c\n", *p2);   // prints the last letter

    return 0;
}
于 2013-10-29T21:56:47.120 回答