我正在查看指针以及如何访问指针指向的内存位置的内容。
我的问题是:在 C 中访问指针的值时,间接引用和取消引用之间有什么区别吗?
我对装配很新,并且有一些非常基本的问题.
这四个命令有什么区别?
mov ebx, eax
mov [ebx], eax
mov ebx, [eax]
mov [ebx], [eax]
Run Code Online (Sandbox Code Playgroud)
他们说括号的意思是"得到地址的价值".但那么,第一线真正做到了什么呢?它不会将eax的价值转移到ebx吗?如果是这样,那么括号的重点是什么?
这有效:
void foo(int (&a)[3]) {
auto ibegin = begin(a);
auto ebegin = end(a);
}
Run Code Online (Sandbox Code Playgroud)
虽然这不是:
void foo(int (*a)[3]) {
auto ibegin = begin(a);
auto ebegin = end(a);
}
Run Code Online (Sandbox Code Playgroud)
我考虑int (&a)[3]并int (*a)[3]具有相同的含义!
我在学习C方面遇到了一些问题,而且我真的没有其他地方可以提出建议.我来自一系列OOP语言,例如JavaScript和主要是Python,因此C是一个重大变化,我在尝试学习基础知识方面遇到了一些障碍.我最初是从Zed Shaw的"艰难学习C"开始的,但他并没有在书中教授任何东西.是的,他让你写了很多代码并改变了一些东西,但是我不知道为什么代码会工作,而这只会导致更复杂的例子.
我遇到的主要问题是变量和指针之间的区别(我认为它是非常独特的,直到我看到一些我将在下面发布的例子,这完全模糊了两者之间的界限).
例如,我理解声明和初始化一个int被调用的a和一个指针,p看起来像这样:
int a;
int *p;
a = 12;
p = &a;
Run Code Online (Sandbox Code Playgroud)
令我困惑的是当你声明看起来像指针的变量,但根本不是指针(或者它们是什么?).例如:
char *string = "This is a string";
printf("%s\n", string);
Run Code Online (Sandbox Code Playgroud)
什么string时候定义和初始化?它是指针吗?如果是的话,为什么不在printf函数中打印它时取消引用它?有很多像这样的例子让我很困惑.
我遇到的另一个例子没有任何意义:
int i;
scanf("%d", &i);
Run Code Online (Sandbox Code Playgroud)
i当&符号应该引用变量的内存中的位置而不是值时,这个函数如何更新整数的值?阵列和结构变得更加复杂,这是我停下来的地方,我决定要找一些建议.
老实说,我觉得很尴尬发布这样一个菜鸟问题,但每个人都从某个地方开始.这些基础知识是我知道在继续之前需要能够理解的东西,但是当我看到与我刚刚学到的内容相矛盾的代码示例时,我很难理解它.我知道这是一个非常普遍的问题,但我希望你们中的一些人能够解释这些基础知识,或者指出我可以更好地学习/理解这一点的方向.我遇到的大多数视频教程都过于笼统,与在线文本教程相同,他们告诉你如何做某事,但不解释它,这会导致一些严重的问题.
我正在做我的 C 入门课程作业,我的任务是以下......
虽然我的知识很基础,但我相信我理解主要内容
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%f\n", &first);
printf("Enter your second number\n");
scanf("%d\n", &second);
//call the function, first and second by …Run Code Online (Sandbox Code Playgroud) 如果我创建一个空指针,并将一段内存分配给该空指针,我如何打印出我刚刚分配的各个位?
例如:
void * p;
p = malloc(24);
printf("0x%x\n", (int *)p);
Run Code Online (Sandbox Code Playgroud)
我想上面打印我刚刚分配的 24 位。
我正在使用 GSL 研究非线性微分方程。问题是我对 C 语言很陌生。我只是将 GNU 站点上的示例修改为我现在感兴趣的方程式。
这是等式:
d2x/dt2 + r*dx/dy + cos(x) + v*cos(2*x+0.4) E1*sin(wt) + E2*sin(2*w*t+a) = 0
Run Code Online (Sandbox Code Playgroud)
我被卡住的是我不知道如何在代码中插入多个参数。此外,我不知道如何在这段代码中使用余弦或正弦函数。
我一直在谷歌上搜索,试图找出这个问题。我找不到任何可以帮助我的东西。
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <math.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>
int func (double t, const double x[], double y[], void *params)
{
double r = *(double *)params;
double v = *(double *)params;
double w = *(double *)params;
double E1 = *(double *)params;
double E2 = *(double *)params;
double a = *(double *)params;
y[0] = x[1];
y[1] …Run Code Online (Sandbox Code Playgroud) 我创建了一个程序,其中我注意到两件事
我的代码:
#include<stdio.h>
int main ()
{
char str[10]="PinkFloyd";
char *s;
char **s1;
s=&str[0];
s1=&s;
printf("the word is using pointer to pointer %s",*s1); //why if I used %c does not print the first character
printf("\n");
printf("the word is using s pointer %s",s); // why if I had replaced with *s does not print anything
return 0;
}
Run Code Online (Sandbox Code Playgroud) 当教授说这ptr=&x表示一个变量ptr分配变量的地址时,我正在观看讲座并感到困惑 x.并且因为y=*ptr+1 他*ptr表示存储在x(或值x)的值.我在这里稍微感到困惑,因为*ptr应该指向x正确的地址,而不是存储在的值x?有人可以再详细说明一下吗?
以下程序输出是正确的,但是当我使用它代替*this时会出现错误.任何人都可以告诉我这是什么和*这意味着什么
#include<iostream>
using namespace std;
class Test
{
private:
static int count;
public:
Test& fun(); // fun() is non-static now
};
int Test::count = 0;
Test& Test::fun()
{
Test::count++;
cout<<Test::count<<" ";
return *this;
}
int main()
{
Test t;
t.fun().fun().fun().fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1 2 3 4
Run Code Online (Sandbox Code Playgroud)
当我使用它代替*这个错误来:
In member function 'Test& Test::fun()':
invalid initialization of non-const reference of type 'Test&' from an rvalue of type 'Test*'
return this;
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这和*之间的区别是什么?