我做了2个项目,第一个用C语言,第二个用C++编写,两个项目都有相同的行为.
C项目:
header.h
int varGlobal=7;
Run Code Online (Sandbox Code Playgroud)
main.c中
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void function(int i)
{
static int a=0;
a++;
int t=i;
i=varGlobal;
varGlobal=t;
printf("Call #%d:\ni=%d\nvarGlobal=%d\n\n",a,i,varGlobal,t);
}
int main() {
function(4);
function(6);
function(12);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++项目:
header.h
int varGlobal=7;
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "header.h"
using namespace std;
void function(int i)
{
static int a=0;
int t=i;
a++;
i=varGlobal;
varGlobal=t;
cout<<"Call #"<<a<<":"<<endl<<"i="<<i<<endl<<"varGlobal="<<varGlobal<<endl<<endl;
}
int main() {
function(4);
function(6);
function(12);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我读到默认情况下全局变量是extern,在C++中默认是C和static ; 那么为什么C++代码有效呢?
我的意思是int …
clock_t,time_t和struct tm有什么区别?
struct tm看起来像这样:
struct tm{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
Run Code Online (Sandbox Code Playgroud)
但是clock_t和time_t怎么样?
如果我将一个数组写入输出文件并关闭文件,然后再次打开文件并读取所有文件,直到文件结束,尽管该文件只包含4个数字,程序将读取并打印5个数字,为什么?
节目输出:
a[0] = 4
a[1] = 7
a[2] = 12
a[3] = 34
a[4] = 34
Run Code Online (Sandbox Code Playgroud)
save.bin(使用十六进制编辑器)
04000000 07000000 0C000000 22000000
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <stdlib.h>
#define path "save.bin"
int main(void)
{
FILE *f=NULL;
int a[]={4,7,12,34},i,n=4,k;
f=fopen(path,"wb");
if(f==NULL)
{
perror("Error");
exit(1);
}
for(i=0;i<n;i++) // or I could use fwrite(a,sizeof(int),n,f);
fwrite(&a[i],sizeof(int),1,f);
fclose(f);
f=fopen(path,"rb");
if(f==NULL)
{
perror("Error");
exit(1);
}
i=0;
while(!feof(f))
{
fread(&k,sizeof(int),1,f);
printf("a[%d] = %d\n",i,k);
i++;
}
printf("\n");
fclose(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图让替换所有出现的功能str1在文本t与str2,但我不断收到一个"缓冲区溢出"错误消息.你能告诉我我的功能有什么问题吗?
#include <stdio.h>
#include <string.h>
#include <assert.h>
//replace all *str1 in *t with *str2, put the result in *x, return *x
char * result(char *str1,char *str2,char *t)
{
char *x=NULL,*p=t,*r=t;
x=malloc(400*sizeof(char));
assert(x!=NULL);
x[0]='\0';
r=strstr(t,str1); //r is at the first occurrence of str1 in t, p is at the beginning of t
while(r!=NULL)
{
strncat(x,p,r-p); //copy r-p chars from p to x
strcat(x,str2); //copy str2 to x
p=r+strlen(str1); //p will be at the …Run Code Online (Sandbox Code Playgroud) int * matrixsum(int *a,int *b,int n,int m)
{
int *p=NULL,i,j;
p=malloc(sizeof(int)*n*m);
if(p==NULL)
{
printf("Error!\n");
exit(1);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
*(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);
}
}
return p;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是关于这条线*(p+i*n+j)=*(a+i*n+j)+*(b+i*n+j);:如果我用它替换它,p[i][j]=a[i][j]+b[i][j];我会收到以下错误 3 次:
错误:下标值既不是数组也不是指针也不是向量
为什么?据我所知,它们是一回事。
我的编译器是 gcc 版本 4.6.3。
我正在尝试打印一个包含4个元素的char数组作为浮点数.编译器(gcc)不允许我z.s={'3','4','j','k'};在main()函数中写入,为什么?
#include <stdio.h>
union n{
char s[4];
float x;
};
typedef union n N;
int main(void)
{
N z;
z.s[0]='3';
z.s[1]='4';
z.s[2]='j';
z.s[3]='k';
printf("f=%f\n",z.x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面程序的输出是:f=283135145630880207619489792.000000,一个比浮点变量大得多的数字可以存储; 输出应该是科学记数法4.1977085E-8.那有什么不对?
class matrix
{
int n;
double **a; //the "matrix"
public:
matrix(int);
~matrix();
int getN();
matrix& operator=(matrix&);
double& operator()(int,int);
friend matrix& operator+(matrix,matrix);
friend matrix& operator-(matrix,matrix);
friend matrix& operator*(matrix,matrix);
friend ostream& operator<<(ostream &,const matrix &);
};
matrix& operator+(matrix A,matrix B)
{
int i,j,n=A.getN();
assert(A.getN()==B.getN());
matrix *C=new matrix(A.getN());
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
(*C)(i,j)=A(i,j)+B(i,j);
}
}
return *C;
}
Run Code Online (Sandbox Code Playgroud)
这是重载算术运算符的正确方法吗?
我的代码中是否有内存泄漏?
的构造在堆分配内存,第一对的阵列双指针,则对于每个指针的阵列双.
我试图制作一个包含以下内容的DLL:
基本模板类,只有虚拟析构函数而没有属性(我称之为MatrixInterface)
带有构造函数,析构函数,运算符=和属性(矩阵类)的派生类
一个返回基类指针到新派生对象的函数:
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
template<class T>
MatrixInterface<T> DLL_EXPORT * CreateMatrixInstance(unsigned int n,unsigned int m)
{
return new matrix<T>(n,m);
}
Run Code Online (Sandbox Code Playgroud)
我想使用这个函数在我的程序中实现矩阵类,但我不能为这个函数分配一个函数指针,我不明白为什么.我可以通过这种方式加载任何不是模板函数的函数.
#include <windows.h>
#include <iostream>
using namespace std;
template<class T>
class MatrixInterface
{
public:
virtual ~MatrixInterface(void);
};
typedef MatrixInterface<int>* (*Fptr)(unsigned int,unsigned int);
int main(int argc, char* argv[])
{
Fptr p;
MatrixInterface<int> *x;
char path[]="basicmatrix.dll";
HINSTANCE hDll = LoadLibrary(path);
cout<<(char*)path<<endl;
if(hDll)
{
cout<<"Library opened succesfully!"<<endl;
p …Run Code Online (Sandbox Code Playgroud) 如果我声明一个类,(没有动态内存分配,没有指针):
class A{
int a,b;
public:
A();
A(int,int);
A& operator=(const A);
};
Run Code Online (Sandbox Code Playgroud)
声明复制构造函数是否安全?默认的复制构造函数如何?
A& A::operator=(const A other)
{
a=other.a;
b=other.b;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
即使我没有声明复制构造函数,当我调用operator =()时也会调用默认的构造函数
编辑:
默认的析构函数是:
A::~A(){}
Run Code Online (Sandbox Code Playgroud)
所以这里不需要它
可能重复:
从函数返回多个值
例如,如果您想要一个修改3个指针值的函数,那么您需要将双指针声明为函数参数.如果用双指针写多行,代码将很难理解; 那么有什么方法可以返回多个值,例如3个输入变量和2个输出变量?
int * function(int *p,int **q,int **r)
{
...
return p;
}
int main(){
int *p,*q,*r;
...
p=function(p,&q,&r);
...
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我试图从WM_TIMER触发WM_PAINT消息; 计时器工作,但RedrawWindow()函数似乎没有做任何事情.我究竟做错了什么?
这是我的回调函数:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
PAINTSTRUCT Ps;
COLORREF clrBlue = RGB(25, 55, 200);
RECT Recto = { 20, 28, 188, 128 };
COLORREF clrAqua = RGB(128, 255, 255);
COLORREF clrRed = RGB(255, 25, 5);
static bool x = true;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); …Run Code Online (Sandbox Code Playgroud)