字符串到整数数组 [英] String to array of integers
问题描述
再次问好,
这仍然与我以前的帖子有关,但是关于同一个程序的另一个问题...
我将字符串转换为整数数组时遇到一些麻烦...例如
string = 12345
整数[0] = 5
整数[1] = 4
整数[2] = 3
整数[3] = 2
整数[ 4] = 1
这是我目前的功能...
再次问好,
这仍然与我以前的帖子有关,但是关于同一个程序的另一个问题...
我在将字符串转换为整数数组时遇到了一些麻烦...例如
string = 12345
整数[0] = 5
整数[1] = 4
整数[2] = 3
整数[3] = 2 >
整数[4] = 1
这是我目前的功能......
展开 | 选择 | Wrap | 行号< /跨度>
你也不要将对malloc的调用结果强制转换为(struct integer *)。不确定这是否会导致你的问题,但它不会有什么问题。
你在atoi中使用的下标call是derefferencing指针。你应该使用指针算法来传递实际的指针。
例如
ar [i]是一个值而(ar + i )是一个指针。
AHHHHH我明白了!这比我做的方式好多了:P谢谢你太多了!
一个更快的问题,我需要我的数字向后存储,因为我在第一个帖子。使用指向字符串的特定索引的指针和atoi()它将整个字符串转换为整数...例如......
string =" 12345"
整数[0] = 5
整数[1] = 45
整数[2] = 345
整数[3] = 2345
整数[4] = 12345
是否有更好的方法进行此转换,以便每个索引只保留1位数? / BLOCKQUOTE>
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...
Expand|Select|Wrap|Line Numbers解决方案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...
Expand|Select|Wrap|Line Numbers
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?
这篇关于字符串到整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆