我是 C 语言的新手。我知道线程是如何工作的,但我想我仍然不明白指针如何与 char 数组一起工作,如何用循环填充数组......
终端上的错误如下...
q2.c: In function ‘main’:
q2.c:18:22: warning: multi-character character constant [-Wmultichar]
q2.c:23:57: warning: multi-character character constant [-Wmultichar]
q2.c:23:40: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
In file included from q2.c:4:0:
/usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
q2.c: In function ‘myfunc1’:
q2.c:61:23: error: invalid type argument of unary ‘*’ (have ‘int’)
ubuntu@ubuntu-VirtualBox:~/Desktop$ gcc q2.c -lpthread -o hell
q2.c: In function ‘main’:
q2.c:18:22: warning: multi-character character constant [-Wmultichar]
q2.c:23:57: warning: multi-character character constant [-Wmultichar]
q2.c:23:40: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast [enabled by default]
In file included from q2.c:4:0:
/usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ but argument is of type ‘int’
q2.c: In function ‘myfunc1’:
q2.c:61:23: error: invalid type argument of unary ‘*’ (have ‘int’)
代码:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
void *myfunc1(void *ptr);
void *myfunc2(void *ptr);
pthread_mutex_t lock;
char name[10];
int id[10];
int i;
int main (int argc, char argv[])
{
memset(name, 'no' , sizeof(name));
memset(id, 0, sizeof(id));
for(i=0; i<10; i++)
{
strcpy(&name[i], 'name');
id[i] = i;
}
//name[10] = '\0';
pthread_t thrd1, thrd2;
int thret1, thret2;
char *msg1 = "First Thread";
char *msg2 = "Second Thread";
thret2 = pthread_create(&thrd2, NULL, myfunc2, (void *)msg2);
thret1 = pthread_create(&thrd1, NULL, myfunc1, (void *)msg1);
pthread_join(thrd1, NULL);
pthread_join(thrd2, NULL);
printf("\nthret1 = %d\n", thret1);
printf("\nthret2 = %d\n", thret2);
sleep(5);
printf("Parent Thread exiting...\n");
exit(1);
return 0;
}
void *myfunc1(void *ptr){
int i;
char *msg = (char *)ptr;
printf("\nMsg : %s\n", msg);
pthread_mutex_lock(&lock);
for(i=0; i<10; i++)
{
printf("\n %s ", *name[i]);
}
pthread_mutex_unlock(&lock);
}
void *myfunc2(void *ptr){
int i;
char *msg = (char *)ptr;
printf("Msg : %s\n", msg);
pthread_mutex_lock(&lock);
for(i=0; i<10; i++)
{
printf("\n%d ", id[i]);
}
pthread_mutex_unlock(&lock);
}