由于C语言中没有数组这样的东西,以下是否都存储在一个内存位置,或者每个元素的值存储在内存位置的"数组"中?
int array[] = {11, 13, 17, 19};
Run Code Online (Sandbox Code Playgroud)
场景1
{11, 13, 17, 19} --> location A
Run Code Online (Sandbox Code Playgroud)情景2
{
11 --> location A
13 --> location B
17 --> location C
19 --> location D
}
Run Code Online (Sandbox Code Playgroud)哪一个是有效的内存布局?
样本1:
char a []={'h','i'};
int i;
for(i=0;a[i]!='\0';i++){
printf("%c",a[i]);
}
printf("%s",a);
Run Code Online (Sandbox Code Playgroud)
输出:hi☻hi♥
样本2:
char a []={'h','i'};
int i;
for(i=0;a[i]!='\0';i++){
char l = a[i];
printf("%c",a[i]);
}
printf("%s",a);
Run Code Online (Sandbox Code Playgroud)
输出:HII♥喜♥♦
样本3:
char a [5]={'h','i'};
int i;
for(i=0;a[i]!='\0';i++){
printf("%c",a[i]);
}
printf("%s",a);
Run Code Online (Sandbox Code Playgroud)
输出:hihi
为什么这三个程序的输出不同?
样品1和样品2几乎是类似的代码,除了额外的线char l = a[i],样品3与样品1和2的不同,基于阵列大小的声明.
我正在学习C,由于某种原因,我%s不会打印出类似的东西.它只是随机字符,%d它总是打印一个像4600688的数字,而不是像我设置的那样12:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("why is %s happening to me"), "this";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这导致:
为什么PE会发生在我身上
(和return文字).
这是编译器错误还是我该如何解决?
我试图弄清楚如何编写一个宏,将一个变量的值附加到字符串.这是一个非工作代码的片段,但我正在展示它,以便我可以解释我想要做什么
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#define DATA_RESPONCE_0 23
#define DATA_RESPONCE_1 24
#define DATA_RESPONCE_2 25
#define DATA_RESPONCE_3 26
#define my_macro(x) DATA_RESPONCE_##x
int main() {
int i = 0;
int k;
k = my_macro (i);
cout << k;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,宏被扩展为DATA_RESPONCE_i,但我希望它是DATA_RESPONCE_0,因此23应该打印为k的值.
我的问题是:在代码块上以调试模式运行的C程序是否有可能删除计算机上的重要数据?(比如更改指针的值而不是指向它的值,然后取消引用它)
我的理解是fgets()返回输入的字符串参数,但是,
我收到"错误:从类型'char*'"分配类型'char [101]'时出现不兼容的类型,
那么,为什么变量'line'被认为是'char [101]'而不是'char*'?
char line[101] = "";
while (feof(filePtr) == 0){
line = fgets(line, 101, filePtr);
strcpy(strPtr, line);
}
Run Code Online (Sandbox Code Playgroud) 我有一个头文件,声明这些数组:
int stVal_On[] = {2};
int stVal_Off[] ={1};
int subVal_On[]={1};
int subMss[]={1};
int subVal_Off[]={0};
Run Code Online (Sandbox Code Playgroud)
然后在声明的结构中使用解除引用的数组:
WriteData结构的定义:
/* Write structure used in loop for Read- and Write Tests */
typedef struct WriteData {
char* name; // MMS object name
const VOID* data; // Data to write
const SINT32 localFormat; // SVI type (on server)
const SINT32 dataLength; // length of data to write/read
const SINT32 NbrofElmnts; // Number of elements to write/read
char* description; // SVI type as String (on server) …Run Code Online (Sandbox Code Playgroud) 在计算圆[3] [6]时,程序似乎在第54行崩溃.可能是什么导致了这个?我输入了一堆打印语句来查找一些信息,所有内容似乎都很好,直到完全[3] [6],这似乎没什么特别的,它崩溃.
int main() {
double a, b, circleXMax, circleYMax, circleRMax;
double rand_float(double a, double b);
int i, j;
int circleInfo = 3;
int circleNum = 50;
int PI = 3.14;
double circleArea;
double circleAreaMax = 0;
double circles[circleInfo][circleNum];
printf("Opened \n");
for(i = 1; i <= circleNum; i++) {
printf("Start of i = %d \n", i);
for(j = 1; j <= circleInfo; j++) {
printf("Start of j = %d \n", j);
if(j == 1 || j == 2) { …Run Code Online (Sandbox Code Playgroud) 如何在不使用C中的头文件的情况下编写hello world?
#include<conio.h>
#include<stdio.h>
void main(){
printf("Hello World");
getch();
}
Run Code Online (Sandbox Code Playgroud)
这是带头文件的简单C程序......
conio.h - 用于控制台stdio.h- 用于printf和scanf所以从我的理解指针变量指向一个地址.那么,以下代码如何在C++中有效?
char* b= "abcd"; //valid
int *c= 1; //invalid
Run Code Online (Sandbox Code Playgroud)