我收到警告:在第2,4行上取消NULL指针'ch'
我不明白为什么.有人可以帮我吗?
char *my_alloc(size_t size) {
char *ch = (char *)malloc(size);
//FIXED: If malloc fails -> exit program
if(*ch == NULL){
exit(0);
}
return ch;
}
Run Code Online (Sandbox Code Playgroud) 为什么我有时会出现这个错误?在字符串eq中使用未初始化的值$ _
if (defined($infos->{"valeur_exclude"}) &&
grep { $number eq $_ } @{$infos->{"valeur_exclude"}});
Run Code Online (Sandbox Code Playgroud)
谢谢
我试图从一个方法获取用户输入并在另一个方法中使用它.我对错误感到困惑,因为它们都是int类型.
public static void move()
{
System.out.println("What do you want to do?");
Scanner scan = new Scanner(System.in);
int userMove = scan.nextInt();
}
public static void usersMove(String playerName, int gesture)
{
int userMove = userMove.move(); //error is here
if (userMove == -1)
{
break;
}
Run Code Online (Sandbox Code Playgroud) 这是我运行的程序:
#include <stdio.h>
int main(void)
{
int y = 1234;
char *p = &y;
int *j = &y;
printf("%d %d\n", *p, *j);
}
Run Code Online (Sandbox Code Playgroud)
我对输出有点困惑.我所看到的是:
-46 1234
Run Code Online (Sandbox Code Playgroud)
我把这个程序写成了一个实验,不知道它会输出什么.我期待可能有一个字节y.
这里发生了什么"幕后"?解除引用如何p给我-46?
正如其他人指出的那样,我必须进行明确的施法才能导致UB.我没有改变这一行char *p = &y;,char *p = (char *)&y;所以我没有使下面的答案无效.
此程序不会导致此处指出的任何UB行为.
long cread(long *xp) {
return (xp? *xp : 0);
}
Run Code Online (Sandbox Code Playgroud)
它无效,因为它可能尝试从空地址读取
所以解决方案建议使用此代码
long cread_alt(long *xp){
long tem = 0;
if(*xp > 0){
tem = *xp;
}
return tem;
Run Code Online (Sandbox Code Playgroud)
但我认为它也无效,因为if(*xp > 0)当xp指向空地址时仍然有缺陷.
所以我想到了这段代码
long cread_alt2(long *xp){
long tem = 0;
if(xp != NULL){
tem = *xp;
}
return tem;
}
Run Code Online (Sandbox Code Playgroud)
我有这个吗?
我正在使用 c 学习递归,并且有一个关于数组的问题,例如:
void main(){
char str[100];
--snip--
if(*str == 'c')
count++;
--snip--
}
Run Code Online (Sandbox Code Playgroud)
这里的 *str 是从指向字符串str 的第一个字符的指针中检索到的值。
我的问题是什么是指针取消引用,我可以执行以下操作:
--snip--
str+=1;
--snip--
Run Code Online (Sandbox Code Playgroud)
获取指向该字符串中另一个字符的下一个位置的新指针?
int main()
{
string s("some string");
if (s.begin() != s.end())
auto it = s.begin();
*it = toupper (*it) ; // Error ; the identifier "it" is undefined
}
Run Code Online (Sandbox Code Playgroud)
为什么*它未定义?为什么我们需要在迭代器维度中使用dereference?
#include<stdio.h>
int main()
{
char *p = NULL;
char str[] = "How do you do!!!!!";
p = &str;
printf("String is:%s",p);
p = "HELLO HOW ARE YOU";
printf("String is:%s",p);
printf("Hello");
int a = 10;
int *pa;
pa = &a;
printf("Contents of a is %d\n",a);
printf("Contents of pa is %x\n", (int)pa);
printf("Values of variable pointed to by pa is %d\n", *pa);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在参考上面的代码时:
p = &str;
printf("String is:%s",p);
Run Code Online (Sandbox Code Playgroud)
p将显示存储的字符串.
pa = &a;
printf("Values of variable pointed to by pa is …Run Code Online (Sandbox Code Playgroud)