标签: function-declaration

strcpy 通过字符串数组传递给函数?

长话短说,我正在制作这个 3D OpenGL 构建器游戏。细节并不重要,但这是我的问题。我有这个字符串数组:

char building_category_names_UPPER_CASE[][30] = { "NOTHING", "WAREHOUSE", "PROCESSING PLANT", "FACTORY", "OFFICE" }; 
Run Code Online (Sandbox Code Playgroud)

我试图将它们传递给一个函数,该函数接受字符串数组形式的“列表”,并通过 Create_Button 方法为该列表中的每个项目创建按钮。

void Create_Button_List(char** list, int list_size, char* group_name, int pos_x, int pos_y)
{
    for (int i = 1; i < list_size; i++)
    {
        char name[50];
        strcpy(name, list[i]);

        char group[50];
        strcpy(group, group_name);

        Create_Button(name, group, pos_x, i * pos_y, 205, 50, text_color_white, bg_color, text_color_white, bg_color_hover);
    }
} 
Run Code Online (Sandbox Code Playgroud)

但即使 VS 在构建时没有给我任何错误,当它到达 strcpy (name, list[i]); 时我仍然收到访问冲突错误。部分。

错误的图像

这里有什么问题?这不是我应该如何传递二维字符数组吗?或者是我将其传递给 strcpy 的方式有问题?需要明确的是,在本例中 list_size 为 5,所以这是正确的,问题不应该是 for 循环越界。

如果有人可以帮助我,请提前致谢!

c pointers multidimensional-array implicit-conversion function-declaration

3
推荐指数
1
解决办法
139
查看次数

为什么使用 std::istream_iterator 消除括号作为函数声明的歧义?

auto queue = [](string str) {
    istringstream ss(str);
    //std::copy(std::istream_iterator<string>(ss),
    //          std::istream_iterator<string>(),
    //          std::ostream_iterator<string>(std::cout, " "));

    //deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>{});
    deque<string> q(std::istream_iterator<string>(ss), std::istream_iterator<string>());
    return q;
};

Run Code Online (Sandbox Code Playgroud)

为什么编译器会抱怨

括号作为函数声明已消除歧义 [-Wveshing-parse]

如果我用 构建一个容器istream_iterator<string>()

和容器构造函数中的参数有什么区别吗std::copy

c++ declaration ambiguity function-declaration

3
推荐指数
1
解决办法
5325
查看次数

C 函数:数组参数长度

这个问题建立在这个问题的基础上,它描述了以下内容如何等效:

int f(int a[10]) { ... } // the 10 makes no difference
int f(int a[]) { ... }
int f(int *a) { ... } 
Run Code Online (Sandbox Code Playgroud)

在有关函数原型作用域的文档中,提供了以下示例:

int f(int n, int a[n]); // n is in scope, refers to first param
Run Code Online (Sandbox Code Playgroud)

这让我质疑以下内容在多大程度上是等效的:

// 1.
int f(int n, int a[n]) { ... }
int f(int n, int *a) { ... }
// my guess: exactly equivalent

// 2.
int x = 10; int f(int a[x]) { ... } …
Run Code Online (Sandbox Code Playgroud)

c arrays language-lawyer implicit-conversion function-declaration

3
推荐指数
1
解决办法
121
查看次数

c ++:解释这个函数声明

class PageNavigator {
 public:
  // Opens a URL with the given disposition.  The transition specifies how this
  // navigation should be recorded in the history system (for example, typed).
  virtual void OpenURL(const GURL& url, const GURL& referrer,
                       WindowOpenDisposition disposition,
                       PageTransition::Type transition) = 0;
};
Run Code Online (Sandbox Code Playgroud)

我不明白那是什么= 0; 部分......我们想要沟通什么?

c++ syntax function-declaration

2
推荐指数
1
解决办法
229
查看次数

NSInvocationOperation使用params定义选择器

我正在尝试创建NSInvocationOperation,以便它应该使用params调用object的方法

- (void) getImages: (NSRange) bounds
{
    NSOperationQueue *queue = [NSOperationQueue new];
    NSArray * params = [NSArray arrayWithObjects:
          [[NSNumber alloc] initWithInt: bounds.location],
          [[NSNumber alloc] initWithInt: bounds.length]];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                   selector:@selector(loadImagesWithOperation)
                     object:params];

    [queue addOperation:operation];
    [operation release];

}

- (void) loadImagesWithOperation:(NSArray*)bounds {
 NSLog(@"loadImagesWithOperation");
}
Run Code Online (Sandbox Code Playgroud)

此代码与EXC_BAD_ACCESS崩溃.如果我改变要调用的函数的定义

- (void) loadImagesWithOperation {
 NSLog(@"loadImagesWithOperation");
}
Run Code Online (Sandbox Code Playgroud)

一切都变好了.我试图在@selector的代码块中使用不同的语法,如@selector(loadImagesWithOperation :)@selector(loadImagesWithOperation:bounds :),但没有成功.

使用params定义选择器和函数的正确方法是什么?

谢谢.

objective-c selector nsinvocation function-declaration ios

2
推荐指数
1
解决办法
5308
查看次数

类型在haskell中意味着什么

我在课堂上被问到这个问题让我很困惑,我们得到了以下内容:

对于波纹管类型声明:

ranPositions :: Image -> Dims -> [Point] 
getBlockSums :: Image -> Dims -> [Point] -> [BlockSum]
i :: Image
d :: Dims
Run Code Online (Sandbox Code Playgroud)

以下是什么类型的?不是以上吗?!

ranPositions i d
getBlockSums i d
Run Code Online (Sandbox Code Playgroud)

所以我回答的是:

type ranPositions = Array Point Int, (Int, Int)
type getBlockSums = Array Point Int, (Int, Int)

// Because (this was given)

type Image = Array Point Int 
type Dims = (Int, Int)
Run Code Online (Sandbox Code Playgroud)

除了错误之外,这个问题让我很困惑,因为我认为函数的类型是在之后声明的::,因此它已经被赋予了,不是吗?

我可以做一些解释,我真的很感激任何帮助.

theory haskell function-declaration type-declaration

2
推荐指数
1
解决办法
186
查看次数

两个Java函数声明之间的区别

当我遇到两个函数声明时,我正在阅读Hadoop文档,返回对抽象类的引用:

public FSDataInputStream open(Path f) throws IOException
public abstract FSDataInputStream open(Path f, int bufferSize) throws IOException
Run Code Online (Sandbox Code Playgroud)

除了参数的差异,为什么这两个函数有不同的返回类型,一个明确声明abstract而另一个没有?

谢谢.

java abstract function-declaration

2
推荐指数
3
解决办法
248
查看次数

C阵列/指针参数转换

我有一个用C编写的通用问题求解器,它接受一个值数组并就地解决它.该问题被视为固定大小的数组,然后传递给求解函数.切换到使用指针而不是固定大小的数组时,我遇到了一个奇怪的性能问题.

设置代码如下:

int main() {
  int board[256];
  ...
  int *board2 = malloc(sizeof(int) * 256);
  memcpy(board2, board, 256);
  int result = solve_board(___);
}
Run Code Online (Sandbox Code Playgroud)

我已经测试了以下声明和调用(没有关于程序更改的其他内容):

// type 1 - fixed-size array
int solve_board(int board[256]);
solve_board(board);  // 1.167 seconds
solve_board(board2); // 3.760 seconds
// type 2 - pointer
int solve_board(int *board);
solve_board(board);  // 1.173 seconds
solve_board(board2); // 3.529 seconds
Run Code Online (Sandbox Code Playgroud)

solve_board部分是递归的,因此在执行期间会进行多次调用.所有病例的结果都是准确的.

任何人都可以解释为什么使用动态数组需要比传递固定大小的数组更长的时间?

c memory arrays pointers function-declaration

2
推荐指数
1
解决办法
82
查看次数

函数定义与C中的声明不匹配,但在C++中

struct bar以下代码中没有可见的声明或定义时,它会成功编译为C++而不是C:

void foo(struct bar* p);

void foo(struct bar* p){}

int main(){}
Run Code Online (Sandbox Code Playgroud)

编译为C:时出现错误消息error: conflicting types for 'foo'.

谁能解释这种行为?

我已经与两个尝试这样铛++ 3.4克++ 4.8.2-Wall -Wextra -pedantic-errors标志和任一-std=c99-std=c++03C和分别C++.

c c++ function-declaration

2
推荐指数
1
解决办法
420
查看次数

为什么C允许我调用未声明的函数?

我有两个文件:test1.ctest2.c,其中包含main()函数。

test1.c:

#include <stdio.h>  // printf() function declaration/prototype

// function definition
void say_hello() {
  printf("\tHello, world!\n");
}
Run Code Online (Sandbox Code Playgroud)

test2.c:

#include <stdio.h>  // printf() function declaration/prototype
int main() {
  printf("Inside main()\n");
  say_hello();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我的makefile:

a.out: test1.o test2.o
    $(CXX) -o a.out test1.o test2.o

test1.o: test1.c
    $(CXX) -c test1.c

test2.o: test2.c
    $(CXX) -c test2.c
Run Code Online (Sandbox Code Playgroud)

现在应该清楚问题出在哪里:test2.c中的main()函数无需声明就可以调用say_hello()!我运行以下命令以使用gcc编译器: make CXX=gcc 屏幕上显示以下警告:

gcc -c test1.c
gcc -c test2.c
test2.c: In function ‘main’:
test2.c:16:3: warning: implicit declaration of function ‘say_hello’ [-Wimplicit-function-declaration] …
Run Code Online (Sandbox Code Playgroud)

c c++ scope function-declaration

2
推荐指数
1
解决办法
377
查看次数