Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

casting the value of a string into int

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
Hi there; please bare with me as I think this is a really basic question; googled without much help.

how would I cast the "value" of the string into int ?

I have:

char word[20]; // and this variable contains say "hello world"

and I tried the following
int word_int = atoi (word);
int word_int = (int) word;
int word_int = (int) &word;

but not of them seem to be giving the correct result ....
 
You don't cast a string into an [tt]int[/tt]. What value would you expect "hello world" to be?

Given your attempts, I might grudgingly suggest this.
Code:
#include <stdio.h>

int main(void)
{
   char text[] = "hello world";
   int value = *(int*)text;
   printf("value = %d\n", value);
   return 0;
}

/* my output
value = 1819043176
*/
 
It's a while since i programmed in C but I seem to remember that you need to declare:

int atoi();

and then

char a[10];
int i;

strcpy(a, "27");
i = atoi(a);


hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top