我有一个具有以下原型的函数(我无法修改):
void writeData(uint16_t add, uint16_t *data);
Run Code Online (Sandbox Code Playgroud)
*data应该指向一个uint16_t变量。所以在我的代码中,我像这样使用它,例如:
uint16_t dataValue;
dataValue = 55;
writeData(0, &dataValue);
Run Code Online (Sandbox Code Playgroud)
有没有办法简化上面的代码,以便在writeData不声明变量的情况下使用函数dataValue?像这样,但使用正确的语法:
writeData(0, syntaxInCWhichPointsToTheAdressOfTheValueInsideBrackets{55});
Run Code Online (Sandbox Code Playgroud)
如果不是,我们可以修改操作符同一指令行中指向的值&吗?
writeData(0, &dataValue{dataValue=55}); // something like this but with correct syntax
Run Code Online (Sandbox Code Playgroud)
我不想封装writeData在其他函数中。
我偶然发现了这个代码的问题
int arr[10];
scanf("%s", arr);
printf("%s", arr);
Run Code Online (Sandbox Code Playgroud)
我不知道其目的,但我完全知道这段代码有多么难闻。但我的问题是这是否合法,以及我是否可以期望它重新打印我输入的字符串?(前提是输入字符串不要太长而导致缓冲区溢出)
我还想知道是否有任何例子表明这确实有用。
来自关于 fprintf 的 C 标准https://port70.net/%7Ensz/c/c11/n1570.html#7.21.6.1p8
如果不存在 l 长度修饰符,则参数应为指向字符类型数组的初始元素的指针。
和 fscanf https://port70.net/%7Ensz/c/c11/n1570.html#7.21.6.2p12
如果不存在 l 长度修饰符,则相应的参数应是指向足以接受序列的字符数组的初始元素的指针和一个终止空字符,该字符将自动添加。
在这种情况下,它不是一个字符数组。但我似乎记得有一些关于与 char 指针之间的隐式转换的特殊规则,但我也记得这些规则不适用于可变参数函数。
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++)
{
printf("%d",i);
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道为什么程序的结果无论return 0;是否放在代码上结果仍然是正确的。
我一直在尝试理解这两个定义之间的区别以及如何正确使用它们。
在我看来,定义一个二维数组将arr[length][width]矩阵的内容一行一行地连续存储在内存中(矩阵本质上是一大行),这样您就可以用这种方式访问 i,j 坐标中的数字*(arr+width*i+j): 。当将其发送到函数时,可以进行声明,func(int *arr)并且由于二维数组在内存中的表示方式,您访问矩阵内容不会有问题。
而将数组定义为似乎int *arr[];并没有以相同的线性、连续的方式将矩阵的内容存储在内存中。函数的声明必须是func(int **arr) or (int *arr[])。虽然您仍然可以通过坐标 i, j 访问矩阵,如下所示:arr[i][j],但似乎不可能这样做:*(arr+width*i+j)。我的想法是否正确,因为以这种方式定义二维矩阵不会将矩阵的行连续存储在内存中,还是我遗漏了一些东西?
c pointers multidimensional-array implicit-conversion function-declaration
我想在重新解释的类中从父函数调用子函数,如下所示。
例子
#include <iostream>
class A {
public:
void func1() {
// some code
func2();
// some code
}
protected:
virtual void func2() {
printf("class A\n");
}
};
class B : public A {
protected:
virtual void func2() {
printf("class B\n");
}
};
int main() {
A* ab = new A();
ab->func1(); // this print "class A"
B* bab = reinterpret_cast<B*>(ab);
bab->func1(); // this also print "class A"
// I want to print "class B" when I use bab->func1()
} …Run Code Online (Sandbox Code Playgroud) 我不明白为什么下面的 C++ 代码可以工作。在此程序中,用户指定数组的大小和数组本身,然后程序打印数组的长度及其元素。我用来reserve()为数组元素分配内存,据我所知,我必须用来push_back()将元素添加到向量中,但我不这样做,虽然它总是打印length 0,但它访问向量元素没有任何问题并正确打印它们。为什么会这样呢?
#include <iostream>
#include <vector>
using namespace std;
int main(){
unsigned size;
cin >> size;
vector<unsigned long> nums;
nums.reserve(size);
for(unsigned i{0}; i < size; ++i) cin >> nums[i];
cout << "length " << nums.size() << '\n';
for(unsigned i{0}; i < size; ++i) cout << i << ' ' << nums[i] << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
#include <vector>
using namespace std;
int main(){
unsigned size;
cin …Run Code Online (Sandbox Code Playgroud) 我打开了一个二进制文件,如下所示
FILE *p;
p=fopen("filename.format","rb");
Run Code Online (Sandbox Code Playgroud)
如何找到文件末尾?
谁能告诉我这个程序的缺陷......?实际上它是两次打印最后一条记录.
#include <stdio.h>
int main(void)
{
int accountNum;
char name[30];
double balance;
int counter = 0;
FILE *clientDataFile1;
if( (clientDataFile1 = fopen("clients.txt", "r")) == NULL )
printf("File could not be opened");
else
{
printf("%-10s %-13s %s\n", "Account", "Name", "Balance");
while( !feof(clientDataFile1) )
{
fscanf(clientDataFile1, "%d%s%lf", &accountNum, name, &balance);
printf( "%-10d%-13s%.2lf\n", accountNum, name, balance );
}
printf("\n\n\n");
rewind(clientDataFile1);
counter++;
fclose(clientDataFile1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这真的很痛苦.我尝试了很多次但是这个漏洞无法捕捉.要么工作不清楚,要么Ubuntu 12.10或gcc对此负责.帮我....
#define SIZE 7000
static char buf[SIZE];
static char *bufptr = buf;
struct node
{
int reg_num;
int val;
char var_name[30];
char var_str[100];
struct node *memroy;
struct node *next;
};
struct node* add(struct node *head, int i)
{
struct node *temp;
if (head == NULL)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->next = NULL;
temp->reg_num = i;
head = temp;
}
else
{
head->next = add(head->next, i);
}
return head;
}
void* malloc(int n)
{
if (buf + SIZE - …Run Code Online (Sandbox Code Playgroud)