void my_cool_function()
{
obj_scene_data scene;
obj_scene_data *scene_ptr = &scene;
parse_obj_scene(scene_ptr, "test.txt");
}
Run Code Online (Sandbox Code Playgroud)
如果我能做的话,为什么我会像上面那样创建一个指向局部变量的指针
void my_cool_function()
{
obj_scene_data scene;
parse_obj_scene(&scene, "test.txt");
}
Run Code Online (Sandbox Code Playgroud)
以防它是相关的:int parse_obj_scene(obj_scene_data*data_out,char*filename);
我正在玩C,我遇到了这个错误:
#include <stdio.h>
int main ()
{
char* foo;
scanf("%s", foo);
printf("entered %s", foo);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
scanf取指针,foo是指针,但我得到总线错误.我怎样才能使它工作?
我正在尝试做一些非常简单的事情:用C中的execvp调用中的参数填充char**.
这就是我在做的事情:
if(argc >=1)
{
*nargv = "--action";
while(argc--)
{
printf("nargv1 => %s \t argv1 => %s \n", *nargv, *argv);
*++nargv = *argv++;
printf("nargv2 => %s \t argv2 => %s \n", *nargv, *argv);
}
printf("nargv3 => %s \t argv3 => %s \n", *nargv, *argv);
*nargv++ = '\0';
printf("nargv4 => %s \t argv4 => %s \n", *nargv, *argv);
}
Run Code Online (Sandbox Code Playgroud)
输出给了我:
nargv1 => --action argv1 => backup
nargv2 => backup argv2 => --help
nargv1 => backup argv1 => --help
nargv2 => --help …Run Code Online (Sandbox Code Playgroud) 好吧,我在这里查看代码,这个想法很难理解.
#include <iostream>
using namespace std;
class Point
{
public :
int X,Y;
Point() : X(0), Y(0) {}
};
void MoveUp (Point * p)
{
p -> Y += 5;
}
int main()
{
Point point;
MoveUp(&point);
cout << point.X << point.Y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
好吧,所以我相信创建了一个类,声明了X和Y,并将它们放在构造函数中
创建一个方法,参数是Point*p,这意味着我们将把构造函数的指针放在函数内部;
现在我们创建一个名为point的对象然后调用我们的方法并将指针地址放在其中?
指针只是寻址一个像0x255255这样的内存号吗?
为什么没有宣布?
(int * p = Y)
Run Code Online (Sandbox Code Playgroud)
什么是内存地址?它可以用作参数吗?
我正在尝试创建一个类似strlen()string.h 的函数
它给了我错误 can not convert char* to char
#include<stdio.h>
#include<conio.h>
int xstrlen(char string);
void main(void) {
char string[40];
puts("Enter string:");
gets(string);
printf(" %s is the length of %d", string, xstrlen(string));
}
int xstrlen(char string[]) {
int i;
for (i=0; ; i++) {
if (string[i] == '\0')
break;
}// for ends
return i;
}
Run Code Online (Sandbox Code Playgroud) 这是通过引用传递文件指针的正确语法吗?
功能调用: printNew(&fpt);
printNew(FILE **fpt)
{
//change to fpt in here kept after function exits?
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现二进制搜索树操作并在删除时陷入困境.
11
/ \
10 14
Run Code Online (Sandbox Code Playgroud)
使用inorder遍历作为树的表示最初输出是10 11 14.
删除节点10,输出预期为11 14但我得到0 11 14.
删除节点14,输出预期只有11但我得到0 11 67837.
请解释我输出错误的原因.我不是在找任何代码:).
typedef struct _node{
int data;
struct _node *left;
struct _node *right;
} Node;
Node* bstree_search(Node *root, int key)
{
if(root == NULL){
return root;
}
// Based on binary search relation, key can be found in either left,
// right, or root.
if(key > root->data)
return bstree_search(root->right, key);
else if(key < root->data)
return bstree_search(root->left, key);
else
return root;
}
void bstree_insert(Node **adroot, int …Run Code Online (Sandbox Code Playgroud) 我有一个返回float*的函数,它实际上在返回函数中作为数组填充
float* some_fnc()
{
float *x=malloc(sizeof(float)*4);
x[0]=......
}
...
// in main
float y[4];
y=some_fnc();
Run Code Online (Sandbox Code Playgroud)
但是我收到"不兼容的类型"错误,这是正常的吗?有没有办法克服这个w/o声明y为浮动*?
以下程序使用g ++编译,但在运行时崩溃:
class someClass
{
public:
int const mem;
someClass(int arg):mem(arg){}
};
int main()
{
int arg = 0;
someClass ob(arg);
float* sample;
*sample = (float)1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以下程序不会崩溃:
int main()
{
float* sample;
*sample = (float)1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)