Home > OS >  How do I do string indexing inside a thread
How do I do string indexing inside a thread

Time:12-14

I was trying to take string input, send them to thread and convert them into integers and add them. My result should have been 1 1 = 2. instead I get 49 49 = 98

main.c: In function ‘prints’:
main.c:10:7: warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]
   10 |     c = (int *) string;
      |       ^
main.c:11:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   11 |     a = (int *) string[0];
      |         ^
main.c:11:7: warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]
   11 |     a = (int *) string[0];
      |       ^
main.c:12:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   12 |     b = (int *) string[1];
      |         ^
main.c:12:7: warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]
   12 |     b = (int *) string[1];
      |       ^
In function main(): Creating a new thread
11
Given input -1341915086 and sum of 49 and 49 is 98

Here is my code. Initially I took the strings as static, sent them to my thread. The printing works fine and its getting the desired value in string.

#include<stdio.h>
#include <pthread.h>
#include<string.h>
#include <stdlib.h>

void *prints(void *str) {
    int a,b,c, sum;
    char *string = (char *) str;
    printf("%s\n",string);
    c = (int *) string;
    a = (int *) string[0];
    b = (int *) string[1];
    sum =  a b;
    printf("Given input %d and sum of %d and %d is %d",c,a,b,sum);
}

int main(int argc, char *argv[]) {
    pthread_t threadID;
    const char *word = "11"; // string literals are immutable
    printf("In function main(): Creating a new thread\n");
    int status = pthread_create(&threadID, NULL, prints, (char *)word);
    pthread_join(threadID, NULL);
}

CodePudding user response:

My result should have been 1 1 = 2. instead I get 49 49 = 98

The ASCII code for character '1' is 0x31, or 49 decimal (see man ascii).

You want:

  a = string[0] - '0';
  b = string[1] - '0';
  sum = a   b;

Explanation: ASCII codes for characters '0' through '9' are sequential (starting with 0x30 and ending with 0x39), so use '0' as the base to determine the numeric value of a given digit.

CodePudding user response:

Converting a character symbol to an integer is done by string[0] - '0'. Which is basic stuff you should study way before even considering learning multi-threading. Or you can convert a whole string to a single integer with the strtol function.

Apart from that, the problem is that you are doing really strange casts and invalid conversions out of the blue. Syntax-wise, C allows all manner of wild and crazy casts, but that doesn't mean that the end result is well-defined or usable.

  • c = (int *) string; This is nonsense, string is a char* pointing at a character array. It may not be aligned. If you dereference *c you violate "strict aliasing". Undefined behavior, anything might happen.

  • a = (int *) string[0]; This is nonsense since string[0] is a char, not a pointer, Quite possibly the char value can be reinterpreted as a pointer on most systems, but doing so is just completely senseless. Furthermore, you cannot assign a pointer to an int, doing so is invalid C. Please see "Pointer from integer/integer from pointer without a cast" issues Since it is invalid C, also undefined behavior and anything or nothing can happen.

  • Same with b = ....

  • Your function must return or you invoke undefined behavior there as well.

You can avoid all manner of problems like this by listening to compiler warnings, or better yet upgrade them to errors to block your invalid code from generating an executable in the first place. See What compiler options are recommended for beginners learning C?

  • Related