这里没有问题:
int *x;
x = (int *) malloc(0 * sizeof(int));
int value = 5;
x = (int *) realloc(x,(value)*sizeof(int));
Run Code Online (Sandbox Code Playgroud)
但我不能为字符串这样做:\
我想为ay []数组执行此操作,如下所示:
y[0]="hello"
y[1]="how are you"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我正在做的事情:
用户输入两个数字.使用这些数字作为维度声明数组.外面的函数main()正在填充数组.访问数组以获取main()更多内容.
我有什么问题:
函数+数组组合似乎没有像我想的那样工作.
我做了什么:
void tablica1(int h, int w)
{
int m,n;
for(m=0; m<h; m++)
for(n=0; n<w; n++)
{
arr[h][w]=1;
}
}
Run Code Online (Sandbox Code Playgroud)
怎么了:
array arr无法访问,tablica1()因为它尚未在该函数中声明.
当然,当我声明其中的数组tablica1()变得无法访问时main().
可能的解决方案:
tablica1()作为参考 - 不知道如何做到这一点tablica1()并以某种方式将其传递给main()- 不知道该怎么做其他可能的方案?
我想了解从函数调用返回指针的行为.假设我有以下简单代码:
int main(){
int i;
float *ssd;
ssd = test();
for (i = 0; i < 3; ++i) {
printf("%f, ", ssd[i]);
}
printf("\n \n");
memset(ssd, 0.0, 3*sizeof(float));
for (i = 0; i < 3; ++i) {
printf("%f, ", ssd[i]);
}
printf("\n \n");
}
float *test(){
float *buff = malloc(3* sizeof(float));
int i;
for (i = 0; i < 3; ++i) {
buff[i] = (float) (6.31 + i);
}
free(buff);
return buff;
}
Run Code Online (Sandbox Code Playgroud)
如你所见,我在buff里面创建了一个临时缓冲区test().在我回来之前,我buff在 …
我希望编写一个C++模板函数,该函数反过来使用一些"C"函数并利用函数重载.
例如,我需要myAbs使用模板编写一个函数,这些模板根据输入参数类型进行适当的调用fabs或abs定义math.h.这该怎么做?
#include <math.h>
template<typename T>
T abs(T x)
{
// I need to write an efficient code here!
// If it is 'double' and 'float' I may be able to compare the
// sizeof(Type) and call 'return fabs(x)' or 'return abs(x)'.
// But this is not a good solution as two types can be of same size!
}
Run Code Online (Sandbox Code Playgroud)
注意:我只是用它作为例子来解释我的问题.我已经知道这样的功能"abs"已经可用了<cmath>.
如果我使用声明任何变量void,它会给出错误但是sizeof(void)1.这是什么原因?
void var; // it gives error
printf("sizeof(void) = %d", sizeof(void)); //prints 1
Run Code Online (Sandbox Code Playgroud) 我只是在学习 C 和 C++ 编程。
我已经搜索过,似乎无法找到一个有不错回应的答案。当然使用<string>要容易得多,但对于这项任务,我只需要使用 clib<string.h>函数;我也不允许使用 C++11 函数。
我有以下两个变量,并且希望移动的内容buffer进入c。
vector<char> buffer;
char* c = "";
Run Code Online (Sandbox Code Playgroud)
我怎样才能轻松做到这一点?
到目前为止我有这个,但它显然不起作用,否则我不会在这里。
for (int b = 0; b < buffer.size(); b++)
{
c += &buffer[b];
}
Run Code Online (Sandbox Code Playgroud) 对于我的代码
template Signal<float>;
template Signal<bit_t>;
template Signal<byte_t>;
template Signal< std::complex<float> >;
template Signal< int >;
Run Code Online (Sandbox Code Playgroud)
我收到编译错误
error at signal_T.cpp:437: error: expected unqualified-id before â;â token signal_T.cpp:438: error: expected unqualified-id before â;â token signal_T.cpp:439: error: expected unqualified-id before â;â token signal_T.cpp:440: error: expected unqualified-id before â;â token signal_T.cpp:441: error: expected unqualified-id before â;â token
编译器想告诉我什么?
我该如何解决这些错误?