创建变量时,例如:
int x = 5;
它会存储在内存中的某个地方,很酷。
但是,当我通过执行以下操作更改变量的值时:
x = 10;
内存中发生了什么?
x的新值是否会覆盖使用相同内存地址的旧值?
或者新值被存储在新的内存地址中,然后旧地址被删除?
当我遇到指针时,这个问题就出现了。似乎使用指针更改变量的值与使用另一个值定义变量是相同的。
这是我的代码(大部分是注释(lol)):
#include "iostream"
int main()
{
int x = 5; // declaring and defining x to be 5
int *xPoi
class A {
public:
void show() {
cout << "Class A Show"<<'\n';
}
};
class C : public A {
public:
void show() {
cout << "Class C Show" << '\n';
}
};
int main() //first int main()
{
C* a = new C; //line 1
我已经编写了一个代码来打印名称的id和list的元素。
sample_list = [1, 2, 3, 4, 5]
print(f"The id of the name of the list is = {id(sample_list)}")
for i in range(0, len(sample_list)):
print(f"Id of element index {i} is {id(sample_list[i])}")
输出:
The id of the name of the list is = 2874211984576
Id of ele
int main(){
printf("hello world"+2);
}
test.c:32:25: warning: adding 'int' to a string does not append to the string
[-Wstring-plus-int]
printf("hello world"+2);
~~~~~~~~~~~~~^~
test.c:32:25: note: use array indexing to silence this warning
printf(&
可能重复:
参考变量是C++概念吗?有C版的吗?如果在C中可用,为什么我的代码会产生编译错误?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a = 10;
int b = 20;
int &c = a;
int &d = b;
return 0;
}
输出:
bash-3.2$ gcc test.c
test.c: In function `main':
test.c:12: error: parse error before '&
我对C#/编程非常陌生,作为一个学习练习,我完成了一个将文本更改为小写的在线挑战。挑战指定它必须‘打印到标准输出’,但我通过使用Console.Writeline完成了挑战
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lowercase
{
class Program
{
static void Main(string[]
考虑下面这段代码:
int i, k, m;
k = 12;
m = 34;
for (i = 0; i < 2; i++) ((i & 1) ? k : m) = 99 - i;
printf("k: %ld m: %ld\n\n", k, m);
在这个愚蠢的示例中,条件运算符表达式是以下内容的快捷方式:
if (i & 1) k = 99 - i; else m = 99 - i;
我的编译器没有错误,执行这段代码会得到预期的输出
k: 98 m: 99
我的问题是,根据C标准,这是不是有效的代码?我以前从来没有见过这样的东西。
为什么printf打印7,尽管变量a是函数fun()的局部变量,并且一旦控制从fun()函数返回,就不应该再存在。
这是c代码。
#include<stdio.h>
main()
{
int *fun();
int *c=fun();
printf("%d",*c);
getch();
}
int *fun()
{
int a=7;
return(&a);
}
输出:7
我在一个编译器设计课程中读到,扫描的输出是一个序列对(令牌代码,标记在符号表中的位置)。我对“立场”部分的含义有点困惑。当符号表表示为可以使用索引(例如数组)访问其元素的结构时,“位置”是明确的,它意味着数组中的第1、2、99元素。例如,源代码:
if (a == b) a = a + c;
扫描的输出将是流:( .,(id,1),.,(id, 2 ),.,(id,3) ) --为了简单起见,我没有描述其他标记--而符号表将是(a,b,c),因此在符号表的位置3上的位置1,b上有a,c。
当符号表被表示为二进制搜索树时会发生什么?对于相同的源代码,符号表树将有一个根节点,其键'b
下面是我的代码( Mac上的g++编译器):
/* initialize character arrays */
char a[2] = "a";
char b[2] = "b";
/* assigns to c the pointer to a read-only buffer containing 'c' */
char* c = "c"; // does this cause a memory leak?
/* allocate heap-memory for d */
char* d = new char[2];
st
我有一段代码可以在C中工作,但不能在C++中工作,有没有办法让它在C和C++上都能工作呢?
void foo(void* b)
{
int *c = b;
printf("%d\n",*c);
}
int main ()
{
int a = 1000;
foo(&a);
return 0;
}
输出:
C++:
1 In function 'void foo(void*)':
2 Line 3: error: invalid conversion from 'void*' to 'int*'
3
一个简短的例子会输出一个奇怪的结果!
#include <iostream>
using namespace std;
struct A { int a; };
struct B { int b; };
struct C : A, B
{
int c;
};
int main()
{
C* c = new C;
B* b = c;
cout << "The address of b is 0x" << hex << b << endl;
cout <<
很久以前,我曾在C语言里为学校编程。我记得我非常讨厌C的一些东西:未赋值的指针不指向NULL。
我问了很多人,包括老师,为什么在世界上,他们会使未分配指针的默认行为不指向空,因为它看起来比不可预测的危险得多。
答案应该是表演,但我从来没有相信过。我认为,如果C默认为NULL,编程史上的许多bug是可以避免的。
这里有一些C代码要指出(双关意)我要说的是什么:
#include <stdio.h>
void main() {
int * randomA;
int * randomB;
int * nullA = NULL;
int * nullB = NULL;
我只是在学习关键字new和delete的指针和动态内存分配。
下面是我的C++代码,用来测试我的理解。
#include <iostream>
using namespace std;
int main() {
// Variable to be pointed to by a pointer
cout << "Create a double variable" << endl;
double n = 3.1415926;
cout << "n = " << n &l