2013-10-06 31 views
0

我是新來的C語言。我知道線程是如何工作的,但我覺得我還是沒有得到這個想法指針如何與字符數組,如何填充數組與環...如何從不同的線程訪問數組?

在終端上的錯誤是如下...

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’) 
[email protected]:~/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); 
} 
+0

第一個線程應填寫姓名數組「名字」 ..和第二個線程應填寫ID陣列遞增的數字... – user2228135

+1

你在期待'memset的(名字,「不」,的sizeof (名字));'做什麼? –

回答

0

看起來像是你很混淆了字符串和字符常量。

char name [100]; char c;

始終在「雙引號」中的字符串。一個(單個字符)用單引號括起來。所以,「stackoverflow」是一個字符串,'s','t','a','c','k'是字符。

另外,從字符串變量中,使用[]運算符提取單個字符,如下所示。請注意,我不使用'*'運算符。

char name[10] ; 
char c; 
strcpy (name,"stackoverflow"); 
printf ("name is %s",name); 
print ("first character of name is %c", name[0]); 
+0

明白了:)謝謝你 – user2228135

2
  1. '是指定字符,"被指定字符串。在

    memset(name, 'no' , sizeof(name)); 
    

    您正試圖定義'no'這是c中不允許的。

  2. memset用於設置一個存儲塊的一個字符值。您可能需要memcpystrcpy來初始化名稱。

  3. char name[10];定義字符數組,但如果你想這個定義字符串數組,你將需要char name[10][NAME_LEN];代替(字符長度的任何最大值)。這也應該修復strcpy錯誤(不要使用&符號)。

  4. myfunc1中,您正在提領角色。將它固定到c字符串的數組將有所幫助,但不需要取消引用它來打印它。

+2

''no'' * *在C中是允許的,但它是實現定義的。它們被稱爲多字符常量。 – dreamlax