char b[]="hello,you';b[5]=0;printf(”%\n“,b)
时间: 2024-02-13 21:05:32 浏览: 49
这段代码存在语法错误,应该改为:
```
char b[] = "hello,you";
b[5] = '\0';
printf("%s\n", b);
```
这段代码的作用是将字符串 "hello,you" 中的第6个字符改为字符串结束符 '\0',然后打印输出结果为 "hello"。
相关问题
编制程序,调用指针作为参数的函数,实现下面两字符串变量的交换。 char* ap="hello"; char* bp="how are you"; 交换的结果为:ap 指向"how are you",bp 指向"hello"。 (注意:输出语句中的;和.为英文符号)
可以编写一个函数,接受两个指向字符数组的指针作为参数,实现交换它们所指向的字符串。
具体实现如下:
void swap(char** p1, char** p2) {
char* temp = *p1;
*p1 = *p2;
*p2 = temp;
}
然后在主函数中调用该函数,将指向字符串的指针传递给它:
char* ap = "hello";
char* bp = "how are you";
printf("交换前:ap=%s, bp=%s\n", ap, bp);
swap(&ap, &bp);
printf("交换后:ap=%s, bp=%s\n", ap, bp);
输出结果为:
交换前:ap=hello, bp=how are you
交换后:ap=how are you, bp=hello
修改这段代码,让它能够在gcc编译器中顺利的运行#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> char message[50] = "Hello World"; void *thread_function(void *arg) { printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg); sleep(5); strcpy(message, "HELLO FATHER!"); pthread_exit("===Thank you for your CPU time!"); } int main() { int res; pthread_t threadCH; void *thread_result; res = pthread_create(&threadCH, NULL, thread_function, (void *)message); if (res != 0) { perror("Thread creation failed!"); exit(EXIT_FAILURE); } printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n"); res = pthread_join(threadCH, &thread_result); if (res != 0) { perror("Thread join failed!\n"); exit(EXIT_FAILURE); } printf("CHILD THREAD returned %s\n", (char *)thread_result); printf("Message NOW is %s\n", message); exit(EXIT_FAILURE); }//main
这段代码在我的gcc编译器中可以顺利运行,这里我提供一些修改的建议:
1. 在调用pthread_exit()函数时,需要传递一个void*类型的指针作为线程的返回值,而不是直接传递一个字符串。因此,需要定义一个指针变量,将字符串的地址赋值给它,然后再将指针变量作为参数传递给pthread_exit()函数。
2. 在printf()函数中,需要将换行符“\n”放在双引号内,否则会出现编译错误。
下面是修改后的代码:
```
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
char message[50] = "Hello World";
void *thread_function(void *arg) {
printf("===CHILD run and want to sleep(5). message NOW is %s\n", (char *)arg);
sleep(5);
strcpy(message, "HELLO FATHER!");
char *ret = "===Thank you for your CPU time!";
pthread_exit(ret);
}
int main() {
int res;
pthread_t threadCH;
void *thread_result;
res = pthread_create(&threadCH, NULL, thread_function, (void *)message);
if (res != 0) {
perror("Thread creation failed!");
exit(EXIT_FAILURE);
}
printf("MAIN THREAD is Waiting for thread to finish by JOIN...\n");
res = pthread_join(threadCH, &thread_result);
if (res != 0) {
perror("Thread join failed!\n");
exit(EXIT_FAILURE);
}
printf("CHILD THREAD returned %s\n", (char *)thread_result);
printf("Message NOW is %s\n", message);
exit(EXIT_SUCCESS);
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)