简体   繁体   English

C中的字符串指针显示奇怪的符号

[英]String pointer in C prints weird symbol

I was having some difficulties when trying to print out the string pointer after dynamically insert a character at the front of char array. 在char数组的前面动态插入一个字符后,尝试打印出字符串指针时遇到一些困难。

The parameter *str is a dynamic char array from my main whereas the input is a single character which should append to the first element of the dynamic array after executing the insert(). 参数* str是来自我的main的动态char数组,而输入是单个字符,应在执行insert()之后附加到动态数组的第一个元素。

int main(){ 
//code snippet. I removed other part to keep the question short
printf("How many characters do you want to input: ");
scanf("%d", &n);
str = malloc(n + 1);
printf("Input the string class: ");
scanf("%s", str);

//switch statement
case '1':
    printf("What is the character you want to insert: ");
    scanf(" %c", &input);
    insert(&str, input);
    break;
}
return 0;
}

void insert(char *str, char input) {
    char *new_str;
    int i, len = strlen(str);

    new_str = malloc(len + 1);
    new_str[0] = input;
    strncpy(&new_str[1], str, len - 1);
    new_str[len] = 0;

    for (i = 0; i < len; i++) {
        printf("%c", new_str[i]);
    }
}

When I tried to loop thru the new_str and print out the string array, it gives me weird symbols and I have no idea what are they. 当我尝试遍历new_str并打印出字符串数组时,它给了我奇怪的符号,我不知道它们是什么。 Any ideas? 有任何想法吗?

EDIT 编辑

The expected output as below: 预期输出如下:

How many characters do you want to input: 5
Input the string:datas
The string is: datas
Do you want to 1-insert or 2-remove or 3-quit?: 1
What is the character you want to insert: a
Resulting string: adata

The output I am getting: 我得到的输出:

在此处输入图片说明

Alternative version, avoiding any string copy functions. 替代版本,避免了任何字符串复制功能。 (Since, alter the strlen() you already know the string length to copy, you don't need any more string functions) (由于更改了strlen(),您已经知道要复制的字符串长度,因此不再需要任何字符串函数)

char * insert_a_character(char * str, char ch)
{
char * new;
size_t len;

if (!str) return NULL;
len = strlen(str);

new = malloc (1+len+1);
if (!new) retun NULL;

new[0] = ch;
memcpy(new+1, str, len);
new[len+1] = 0;

return new;
}

I assume that the caller will free if required for orig 我认为如果orig需要呼叫者可以free

char * insert(char *orig, char input) {
   char * new_str = malloc(strlen(orig) + 2); // An extra one for null
   strcpy(new_str + 1, orig);
   new_str[0] = input;
   printf("%s", new_str); // To print it out
   return new_str; // The caller needs to free this;
}

That should work. 那应该工作。

Assembling all comments: 组装所有评论:

void insert(char *str, char input) {
    char *new_str;
    int i, len = strlen(str);

    new_str = malloc(len + 2);
    new_str[0] = input;
    strcpy(new_str+1, str);
    new_str[len+1] = 0;

    for (i = 0; i <= len; i++) {
        printf("%c", new_str[i]);
    }
}

Of course you still need to do something with the new string, such as returning it or freeing it. 当然,您仍然需要对新字符串执行某些操作,例如返回它或释放它。

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

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