How to pass string as parameter to thread in C


dog

I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass string as parameter to thread?

#include <stdio.h>
#include <stdlib.h> // exit calls
#include <pthread.h> // contains thread package

void *print_string_in_reverse_order(void *str)
{
    char *string = (char *)str;
    printf("%s\n", *string); // this won't print anything

    pthread_exit(NULL); // exit the thread
}

int main(int argc, char *argv[])
{
    pthread_t threadID;
    char *word = "word"; //should this be an array?
    printf("In function main(): Creating a new thread\n");

    // create a new thread in the calling process
    int status = pthread_create(&threadID, NULL, print_string_in_reverse_order, (void *)&word);

}
doron

Your problem is passing a pointer to a pointer to a string when using, &wordyou just need to use it wordin the pthread_createparameter .

This is because when you declare

const char* word = "my word";

Minecraft's memory is allocated in read-only global memory, wordthen a becomes a pointer to that memory on the stack. Note that wordthe string cannot be modified even if it is not declared const.

const char word[] = "my word";

Create a large array to represent "my words". In general, it is not safe to pass to another thread because the memory is deleted and then the stack unwinds at the end of the function.

The easiest and safest way to declare a modifiable string is to declare the following:

static char word[] = "my word";

This will ensure "my words" are in global memory and definitely available, otherwise you will need to allocate memory usingmalloc

Related


How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

How to pass string as parameter to thread in C

dog I'm new to C and programming and I'm trying to pass a string to a thread for later manipulation. I tried creating the string using the array char string[] = "word"and passing it to the thread - pointers with char *word = "word"no luck now . How to pass str

Pass monitor method as thread parameter to C++

Andre Winston I'm writing a monitor to mutually exclusive access to " std::listwhat I have now " methods : list<int> l; class monitor{ public: void m_add(int x){ //lock use of m_remove //lock use of m_add l.push_back(x);

Pass monitor method as thread parameter to C++

Andre Winston I'm writing a monitor to mutually exclusive access to " std::listwhat I have now " methods : list<int> l; class monitor{ public: void m_add(int x){ //lock use of m_remove //lock use of m_add l.push_back(x);

Pass monitor method as thread parameter to C++

Andre Winston I'm writing a monitor to mutually exclusive access to " std::listwhat I have now " methods : list<int> l; class monitor{ public: void m_add(int x){ //lock use of m_remove //lock use of m_add l.push_back(x);

How to pass property parameter type using List<string> in C#?

username How do I pass the checklist to the builders? It shows a message: Error 14 Property parameter must be a constant expression, typeof expression or array creation expression of the property parameter type public class CustomAuthorize : AuthorizeAttribute

How to pass string parameter in Python function via C++

Hari Sankar vm The figure shows the output. I hope the word "sea" will be printed. but it prints some numbers how to pass string parameter to python function via C++ I try the code below. It can pass double, float, int, bool values. But when it is passed a str

C - How to pass a space delimited string to a single parameter?

Liam Ryan This is probably a very simple and core component of C programming, but it's very hard to search for it The printk in the kernel can take many log levels of const, and the function itself uses varargs, but I don't know why it works. Here's an example

How to pass string parameter in Python function via C++

Hari Sankar vm The figure shows the output. I hope the word "sea" will be printed. but it prints some numbers how to pass string parameter to python function via C++ I try the code below. It can pass double, float, int, bool values. But when it is passed a str

How to pass string parameter in Python function via C++

Hari Sankar vm The figure shows the output. I hope the word "sea" will be printed. but it prints some numbers how to pass string parameter to python function via C++ I try the code below. It can pass double, float, int, bool values. But when it is passed a str

C - How to pass a space delimited string to a single parameter?

Liam Ryan This is probably a very simple and core component of C programming, but it's very hard to search for it The printk in the kernel can take many log levels of const, and the function itself uses varargs, but I don't know why it works. Here's an example

How to pass string parameter in Python function via C++

Hari Sankar vm The figure shows the output. I hope the word "sea" will be printed. but it prints some numbers how to pass string parameter to python function via C++ I try the code below. It can pass double, float, int, bool values. But when it is passed a str

How to pass string parameter in Python function via C++

Hari Sankar vm The figure shows the output. I hope the word "sea" will be printed. but it prints some numbers how to pass string parameter to python function via C++ I try the code below. It can pass double, float, int, bool values. But when it is passed a str

How to pass Swift array as parameter to thread?

chapka I'm trying to spawn a new thread to perform some background processing based on a String that has been decomposed into an array of characters. Here is what my code looks like: var testString : String = NSString(data:data!, encoding:NSUTF8StringEncodi

How to pass Swift array as parameter to thread?

chapka I'm trying to spawn a new thread to perform some background processing based on a String that has been decomposed into an array of characters. Here is what my code looks like: var testString : String = NSString(data:data!, encoding:NSUTF8StringEncodi

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass string in url parameter?

not any From API i get something like this: processId=22, now i want to pass it in url parameter but the problem is i need to pass key and value. How can i paste the whole string as parameter. Any suggestions? So what I want to achieve is: <a *ngIf="menu.refPa

How to pass string as parameter name?

username I have the following function: def create_act(user, verb, fk_name=None, fk_value=None): fk = getattr(Action, fk_name) action = Action(user=user, verb=verb, fk=fk_value) action.save() Action is a class. The class has multiple properties an

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive

How to pass a modified string parameter?

kaan_a I'm in chapter 12 of the Rust programming language, which implements a case-insensitive line search. It doesn't make sense to me to implement the same logic twice, so I figured out if I just call the case-sensitive search function with a case-sensitive