指针相减

指针相减含义 指针值相减=两个指针之间的元素数目 #include<stdio.h> int main(){ int a[5]={1,2,3,4,5}; int *p=&a[1],*q=&a[4]; printf("%d",q-p); } 运行结果为3 阅读详情
两个同类型的指针相减的差是一个整数=这两个指针的地址值的差除以sizeof(指向的类型)。 
两个不同类型的指针不能相减。看个例子:pointerMinus.cpp
#include <iostream>

int main()
{
    
int s[25];  
    
    std::cout 
<< &s[0] << std::endl;
    std::cout 
<< &s[24] << std::endl; 
    
    
int * p = &s[0] , * t = &s[24];

    std::cout 
<< "sizeof(int)=" << sizeof(int) << std::endl;  
    std::cout 
<< t-p << std::endl;     
}

编译运行结果:
$ g++ -o PM pointMinus.cpp
$ ./PM
0xbfc26630
0xbfc26690
sizeof(int)=4
24

[解释]
两个地址相减:0xbfc26690 - 0xbfc26630 =60 (十六进制的!)即 16*6 = 96(十进制的!)
sizeof(int)=4,t-p = 96/4=24。
C语言技术进阶之 指针相减的含义 int main() { int iA[4]; short *p1 = &iA;[0]; short *p2 = &iA;[1]; printf("Pointer Diff = %lu\n",p2-p1); return 0; } 上述程序运行结果:Pointer Diff = 2 (代表两个USHORT类型) 不是4. 因为指针相减含义是多少个该类型的单位。不是纯字节数。 立即下载

相关推荐

别再手动配网了!用安信可PB-02模组+PHY Mesh App,5分钟搞定蓝牙Mesh智能灯群控

本文详细介绍了如何使用安信可PB-02模组和PHY Mesh App快速搭建蓝牙Mesh智能灯群控系统。通过5分钟的硬件连接和固件烧录,用户可实现多灯分组控制、场景切换及地理围栏等高级功能,显著提升智能家居部署效率。方案特别适合需要快速实现灯光群控的场景。

weixin_28675839的博客 420

指针的运算系列(2):指针-指针(相减)

指针的运算系列(2):指针-指针(相减)

weixin_64308540的博客 3378

Ansys Fluent DPM模型实战:旋风分离器颗粒轨迹仿真全流程解析(附案例文件)

本文详细解析了使用Ansys Fluent中的离散相模型进行旋风分离器颗粒轨迹仿真的全流程。从几何简化、网格策略到物理模型界定,重点阐述了DPM模型的关键设置步骤,包括单向耦合假设、颗粒注入与受力配置,并提供了结果分析与优化方法,帮助工程师精准模拟气固分离过程,提升设计效率。

sunny的博客 705

指针 (三)

如图,这段代码的意义就是:我们先进入到 main 函数当中,创建一个指针 *p ,这个 *p 就是 test 的返回值,然后哦我们进入到 test 函数当中,创建一个变量 a 并给其赋值为 100 ,然后返回 a 的地址,再次回到 main 函数当中,打印 *p ,按理说我们应该是通过 *p 的地址找到 a ,最后打印出100,这看起来好像并没有任何问题,但是请大家注意, test 这里的 a 是一个局部变量,进函数创建,出函数销毁,我们并不能通过 test 的返回值打印出 a。

Dove_Xxx的博客 1824

深入解析基于Fluent的蒸发冷凝相变UDF程序及其在重力热管液体蒸发冷凝变化计算中的应用

ANSYS Fluent是一款功能强大的CFD软件,广泛应用于流体流动、传热、化学反应和多相流等领域的数值模拟。Fluent提供了丰富的物理模型和求解器,支持用户自定义函数(UDF)以扩展其功能,实现更为复杂和特定的模拟需求。UDF是用C语言编写的程序,允许用户在Fluent中自定义物理模型、边界条件、源项等,以实现特定的模拟需求。在处理复杂的相变过程、非均匀物性参数或特定的热传导机制时,UDF提供了极大的灵活性和扩展性。

qq_38334677的博客 1265

华为非AI方向0603笔试真题-爆破小游戏(详细思路+多语言题解)

华为非AI方向0603笔试第二题【爆破小游戏】,提供详细思路讲解以及代码实现。

qq_45776114的博客 358

C语言--指针加减

指针加减一个整数 指针变量的大小都是四个字节,指针加1,是加了一个指针所指向类型的大小,不是加指针变量的大小。 #include <stdio.h> int main() { int m = 0; int *p; p=&m; printf("%p%p",p,p+1);//p+1的意思是p+sizeof(*p)*1 return 0; } 指针-指针 1,指针不适合做加法运算,一般用于减法运算 2,指针做减法运算时,一定要指向同一个数组,相减结果是两个指针之间的元素的数目,而不是两个

zouluhendiao的博客 4882

Chamfer Loss

ChamferLoss是一种常用于3D点云(pointcloud)处理的损失函数,特别是在点云生成和对齐任务中。它的目的是度量两个点集之间的相似度,特别是在点云重建、配准或生成等任务中,常用于优化模型使得生成的点云更接近真实的点云。

夜空中明亮的星的专栏 455

C语言两指针相减计算结果详解

C语言两指针相减计算结果详解

深耕嵌入式领用多年, 致力于分享嵌入式领域技术! 3917

从 SAP BTP trial 到第一个 CPI iFlow 的实操路线图

本文介绍了如何从零开始学习SAP CPI(现称SAP Integration Suite中的Cloud Integration能力)。主要内容包括:1)明确学习边界,建议使用SAP BTP trial账号进行个人练习,注意90天有效期限制;2)理解SAP BTP账号模型层级结构(Global Account→Subaccount→Space);3)详细说明申请trial账号流程,推荐选择Singapore区域以避免兼容问题;4)指导如何检查Entitlements和Quotas,强调应专注于SAP Inte

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 167

034、MLCA 多尺度局部上下文注意力的 YOLOv11 适配:轻量级金字塔式空间上下文聚合

本文介绍了一种将多尺度局部上下文注意力(MLCA)模块集成到YOLOv11模型中的方法,以解决小目标检测中空间信息丢失的问题。MLCA采用金字塔式局部上下文聚合策略,通过不重叠的网格划分实现轻量级空间注意力计算,相比全局注意力机制能更好地保留小目标特征。 作者详细记录了实现过程,包括模块定义、参数调优和训练技巧,特别指出避免使用BN而改用GroupNorm以提升训练稳定性。在VisDrone数据集上的实验表明,MLCA-YOLOv11在参数量和计算量基本不变的情况下,mAP提升2.5%,其中行人检测提升3.

sundong_81的博客 286

两个指针相减

如果两个指针指向同一个数组,它们就可以相减,其结果为两个指针之间的元素数目。      假设我住在广场路124号,Mag住在广场路142号,每家之间的地址间距是2(在我这一侧用连续的偶数作为街道地址),那麽Mag家就在我家往前(142-124)/2家,也就是说可以得到我们两家之间相隔8家。      也就是说可以利用两个指向同一数组的指针相减得到两个指针之间元素的个数。      如果两个指针不是指向同一个数组,它们相减就没有意义了。      C本身无法防止非法的指针减法运算,它无法为你提出任何警

gdmmhym的专栏 3万+

基于深度学习的野生菌菇检测识别分类系统(YOLOv8 + UI界面 + 数据集)

随着深度学习技术的迅猛发展,计算机视觉领域的应用得到了广泛的关注。野生菌菇的检测与识别是一个复杂而又有趣的问题,涉及到物体检测、图像分类以及自然语言处理等技术。本文将介绍如何基于YOLOv8模型,结合自定义UI界面与数据集,构建一个高效且精确的野生菌菇检测识别分类系统。首先,我们将介绍YOLOv8的原理与特点,然后详细描述如何构建和训练野生菌菇识别模型,并介绍如何为该系统设计UI界面,最终形成一个完整的系统。本文内容包括代码实现、模型训练及数据集准备等,旨在为有类似需求的开发者提供参考和帮助。目录摘要目录。

YOLO 1549

两个指针变量可不可以相减?

遇到一道题,问:两个指针变量可不可以相减? 答案是:可以 以前只写过 “指针±整数”,比如数组方面的,两个指针的运算还是第一次碰。 《C和指针》里说: 只有当两个指针都指向同一个数组中的元素时,才允许从一个指针减去另一个指针。 ​两个指针相减的结果的类型是ptrdiff_t,它是一种有符号整数类型。减法运算的值是两个指针在内存中的距离(以数组元素的长度为单位,而不是以字节为单位),因为减法运...

Seven 8186

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

C指针与指针之间的相减操作

指针与指针的相减操作,表示两个指针指向的内存位置之间相隔多少个元素(注意是元素,并不是字节数)。 例如对于int类型的指针p和p1,p1-p的意义表示他们之间相隔多少个int类型的元素。 同样对于其他类型的指针变量之间相减的意义也是一样。 测试代码 #include <stdio.h> int main(int argc,const char * argv[]) { int a[3] = {1,2,3}; int *p = a; printf("---------------

嵌入式 6473

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

指针相减的原理

当两个指针指向同一个数组时,它们相减得到的结果是这两个指针之间相隔的。具体的计算方式为:注意:这句话是没错的,注意理解方式。

2501_92019824的博客 439

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

两个指针相减的功能

某类型指针相减的前提是指向连续的空间地址,相减后的结果是该类型元素的个数。

qq_44423388的博客 388

两个指针相减的结果

例子: inta[3] ={1,2,3};     int* p;     int* q;       p= a;     cout"p的地址:"     cout"p指向的内容:"       q= &a[2];     cout"q的地址:"     cout"q指向的内容:"       cout"两个指针的减法:"     cout"两个指针减法的结果作为数组下

master_feng2009的专栏 5514
上一篇: Blog仍旧频频出错
下一篇: C++的疑问
HateCode
博客等级 码龄20年 25粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值