在C警告:赋值时将指针整数,未作投 [英] Warning in C: assignment makes integer from pointer without a cast
问题描述
我把我的编译程序时收到此错误。这只是我的code的一小部分,所以如果需要的话,我会提供code的其余部分。为什么任何想法正在发生?
无效strip_quotes(char中[]){
如果(S [0] =='')S = S + 1;
如果(S [strlen的(S)-2] =='')S [strlen的(S)-2] = NULL;
}
S [strlen的(S)] = NULL; / * =(无效*)0 * /
有在code另一个bug不会导致一个编译器错误:
如果(S [0] =='')S = S + 1;
的增量小号
不会给调用者可见的,为C的推移值,包括指针(见的 http://c-faq.com/ptrs/passptrinit.html )。纠正选项:
- 使用阵列的内容左移
memmove与()
(或其他一些复制机制) - 传递指针的地址(A
的char **
) - 返回一个指针
取值
修改 S的含量
是preferable因为它避免了如果阵列动态分配可能出现的问题:只有指针由返回的malloc ()
(或释放calloc()
和的realloc()
)可以传递给免费()
。如果取值
被改变,那么就不能免费()
通过 D小号<值/ code>。
需要注意的是:
无效strip_quotes(char中[]){
相当于:
无效strip_quotes(字符* S){
以被指针在code使用柜面你被搞糊涂。
I keep getting this error when compiling my program. This is just a small part of my code, so if needed I will provide the rest of the code. Any ideas on why this is occuring?
void strip_quotes(char s[]) {
if (s[0]=='"') s=s+1;
if (s[strlen(s)-2]=='"') s[strlen(s)-2]=NULL;
}
As Dave has already correctly pointed out the reason for the compiler error:
s[strlen(s)-2]=NULL; /* = (void*)0 */
There is another bug in the code that won't cause a compiler error:
if (s[0]=='"') s=s+1;
the increment of s
will not be visible to the caller, as C passes by value including pointers (see http://c-faq.com/ptrs/passptrinit.html). Options for correcting:
- shift the content of the array to the left using
memmove()
(or some other copy mechanism) - pass the address of the pointer (a
char**
) - return a pointer to
s
Changing the content of s
is preferable as it avoids a possible problem if the array was dynamically allocated: only pointers returned by malloc()
(or calloc()
and realloc()
) can be passed to free()
. If the value of s
is changed then it cannot be free()
d via s
.
Note that:
void strip_quotes(char s[]) {
is equivalent:
void strip_quotes(char* s) {
incase you were confused as to were pointers are used in the code.
这篇关于在C警告:赋值时将指针整数,未作投的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!