c - How can I access arrays from different threads? -


i'm new c language. know how threads work think i'm still not getting idea how pointers works char arrays, how populate arrays loop...

the errors on terminal follows...

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 integer without cast [enabled default] in file included q2.c:4:0: /usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ argument 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 integer without cast [enabled default] in file included q2.c:4:0: /usr/include/string.h:128:14: note: expected ‘const char * __restrict__’ argument of type ‘int’ q2.c: in function ‘myfunc1’: q2.c:61:23: error: invalid type argument of unary ‘*’ (have ‘int’) 

code:

#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); } 

looks confused between strings , characters constants.

char name[100]; char c;

a string in "double quotes". (single character) enclosed in single quotes. so, "stackoverflow" string , 's','t','a','c','k' characters.

also, string variable, extract individual characters using [] operator shown below. note don't use '*' operator.

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

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -