Hello again,
This is still related to my previous post but a different question about the same program...
I'm having some trouble converting a string to an array of integers... such as
string = 12345
integer[0] = 5
integer[1] = 4
integer[2] = 3
integer[3] = 2
integer[4] = 1
This is the function i have currently... -
struct integer* read_integer(char* stringInt){
-
struct integer* intTemp;
-
int i, j, test;
-
intTemp = malloc(sizeof(struct integer));
-
//printf("%s\n", stringInt);
-
intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
-
intTemp[0].size = strlen(stringInt);
-
j = 1;
-
for(i=0;i<intTemp[0].size;i++){
-
intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]);
-
printf("%d.", intTemp[0].digits[i]);
-
}
-
printf("\n");
-
return intTemp;
-
}
-
-
//this is the definition of the struct integer
-
-
struct integer {
-
int* digits;
-
int size;
-
};
-
-
I'm getting a warning from the atoi() function as follows
[Warning] cast to pointer from integer of different size
this has been driving me mad for the past week so any help will be greatly appreciated!
11 2283
Hello again,
This is still related to my previous post but a different question about the same program...
I'm having some trouble converting a string to an array of integers... such as
string = 12345
integer[0] = 5
integer[1] = 4
integer[2] = 3
integer[3] = 2
integer[4] = 1
This is the function i have currently... -
struct integer* read_integer(char* stringInt){
-
struct integer* intTemp;
-
int i, j, test;
-
intTemp = malloc(sizeof(struct integer));
-
//printf("%s\n", stringInt);
-
intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
-
intTemp[0].size = strlen(stringInt);
-
j = 1;
-
for(i=0;i<intTemp[0].size;i++){
-
intTemp[0].digits[i] = atoi(stringInt[intTemp[0].size - (i+1)]);
-
printf("%d.", intTemp[0].digits[i]);
-
}
-
printf("\n");
-
return intTemp;
-
}
-
-
//this is the definition of the struct integer
-
-
struct integer {
-
int* digits;
-
int size;
-
};
-
-
I'm getting a warning from the atoi() function as follows
[Warning] cast to pointer from integer of different size
this has been driving me mad for the past week so any help will be greatly appreciated!
The subscript you're using in the atoi call is derefferencing the pointer. You should use pointer arithmetic in order to pass the actual pointer.
e.g.
ar[i] is a value while (ar + i) is a pointer.
You also do not cast the result of your call to malloc to a (struct integer*). Not sure if this is causing your problems, but it can't hurt.
The subscript you're using in the atoi call is derefferencing the pointer. You should use pointer arithmetic in order to pass the actual pointer.
e.g.
ar[i] is a value while (ar + i) is a pointer.
AHHHHH I see! that works alot better than the way i was doing it :P Thank you sooooo much!
one more quick question though, I need my digits to be stored backwards as I showed in the first post. with using a pointer to a specific index of the string and atoi() it is turning the whole string into integers... such as...
string = "12345"
integer[0] = 5
integer[1] = 45
integer[2] = 345
integer[3] = 2345
integer[4] = 12345
is there a better way of doing this converstion so that each index only holds 1 digit?
You also do not cast the result of your call to malloc to a (struct integer*). Not sure if this is causing your problems, but it can't hurt.
I was taught to do this in my class but then I was told on this forum that casting the malloc can cause problems and doesn't really help at all... So I'm not sure which way is best :\
Thanks :)
AHHHHH I see! that works alot better than the way i was doing it :P Thank you sooooo much!
one more quick question though, I need my digits to be stored backwards as I showed in the first post. with using a pointer to a specific index of the string and atoi() it is turning the whole string into integers... such as...
string = "12345"
integer[0] = 5
integer[1] = 45
integer[2] = 345
integer[3] = 2345
integer[4] = 12345
is there a better way of doing this converstion so that each index only holds 1 digit?
Are you allowed to assume that this will run on an ASCII system? If so, I would just use the fact that there is a direct relation between chars and ints. E.G. the char '0' is equivalent to the int 48 . Then you can just go element by element and fill the int array with the char array's equivalent.
Are you allowed to assume that this will run on an ASCII system? If so, I would just use the fact that there is a direct relation between chars and ints. E.G. the char '0' is equivalent to the int 48 . Then you can just go element by element and fill the int array with the char array's equivalent.
so that would look like
intArray[0] = (int)*-pointerToIndex;
???
so that would look like
intArray[0] = (int)*-pointerToIndex;
???
If you had the string MyString then, you'd do something like -
for(int i = 0; i < MyString.Size; i++)
-
MyIntArray[i] = MyString[i] - 48;
-
If you had the string MyString then, you'd do something like -
for(int i = 0; i < MyString.Size; i++)
-
MyIntArray[i] = MyString[i] - 48;
-
hmm... that seems a bit messy... what I was going to try was to replace the last character in the string with a \0 after i read it and store it as an int, since i won't need that character anymore... however, this seemed to mess things up further
i tried -
strDigits[lastindex] = '\0';
-
//and
-
pointertostringindex = '\0';
-
but neither of these worked, any ideas?
hmm... that seems a bit messy... what I was going to try was to replace the last character in the string with a \0 after i read it and store it as an int, since i won't need that character anymore... however, this seemed to mess things up further
i tried -
strDigits[lastindex] = '\0';
-
//and
-
pointertostringindex = '\0';
-
but neither of these worked, any ideas?
Just free the entire string at the end, if you don't need the data.
The process you describe seems to be re-reading substrings while the method I gave you reads one char at a time.
I was taught to do this in my class but then I was told on this forum that casting the malloc can cause problems and doesn't really help at all... So I'm not sure which way is best :\
I told you about the “don’t cast” rule. It’s not something I made up. Ask the comp.lang.c or comp.std.c people for confirmation.
Hey guys,
Thank you all for your help! I finally got it to work! and here's how in case anyone cares. -
struct integer* read_integer(char* stringInt){
-
struct integer* intTemp;
-
int i;
-
char* pointTo;
-
intTemp = malloc(sizeof(struct integer));
-
//printf("%s\n", stringInt);
-
intTemp[0].digits = malloc(strlen(stringInt) * sizeof(int));
-
intTemp[0].size = strlen(stringInt);
-
for(i=0;i<intTemp[0].size;i++){
-
//sets pointer to index in string
-
pointTo = &stringInt[intTemp[0].size - (i+1)];
-
//reads string from index into digits index i
-
intTemp[0].digits[i] = atoi(pointTo);
-
//sets index of string to '\0' so that the next index of digits doesn't pick it up
-
*pointTo = '\0';
-
}
-
printf("\n");
-
return intTemp;
-
}
-
thank you so much again!
p.s. I wasn't saying i don't believe you about the casting during malloc I just know it works either way now :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Johannes |
last post by:
Hi,
I tried to pack eight integer values and one string into one binary
string, which I would like to store in a mysql db.
I encountered two problems doing this:
1)
$this->packed =...
|
by: David Lawson |
last post by:
I know how to conver a string to an array of strings, but I need to convert an ascii string to an
array of integers (really unsigned chars). Eg,
$str ="ABC";
needs to convert to something...
|
by: B J H |
last post by:
I want to do the following
int list;
String list;
i int;
.....
for (i=0; i<int.length, i++_
{
list.concat(list);
list.concat(" ");
|
by: Graeme Downes |
last post by:
I have a string of integer, such as the following:
"12 24 12 9 2"
and I want to convert this into an array or arraylist which will
seperate the string into an array of integers.
How do I do...
|
by: Pokerkook |
last post by:
Hello,
If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:
49
49
10
49
52
|
by: Dennis Myrén |
last post by:
Hi.
Is there a way to utilize the great primitive data type
formatting routines available in .NET without working with
strings?
I want a byte directly rather than a string.
I think it is...
|
by: comp.lang.php |
last post by:
I'm involved in a rather nasty debate involving a strange issue
(whereby the exasperated tell me to RTFM even after my having done so),
where this is insanely possible:
print_r(is_int('1'));...
|
by: cdg |
last post by:
Could anyone explain how to write this sample program correctly. I need to
convert an integer to a string. However, I would prefer to convert the
integer to char array. But I didn`t want to use...
|
by: Csaba Gabor |
last post by:
I'm comparing the text of (snippets of) web pages which I expect to be
quite different or quite similar. In the case where they are similar,
I would like to display the more recent one and say...
|
by: Pascal |
last post by:
hello and soory for my english
here is the query :"how to split a string in a random way"
I try my first shot in vb 2005 express and would like to split a number in
several pieces in a random way...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |