简体   繁体   English

C ++,需要帮助用strcat创建的指针!

[英]C++, need help with pointers created with strcat!

I need to append a number at the end of the word flare depending on how many I have. 我需要在单词flare的末尾添加一个数字,具体取决于我有多少。 That part works great. 那部分效果很好。 My problem is when I go to print it to the screen. 我的问题是当我把它打印到屏幕上时。 I would like the program to output the value of what (Camera::Table[Id]->sensor->flare1) sensor is pointing at, in this case, flare one. 我想程序输出什么(Camera :: Table [Id] - > sensor-> flare1)传感器指向的值,在这种情况下,是flare one。 If the program were to continue it would output the value pointing at flare2, flare3, flare4,... until the limit is reached. 如果程序要继续,它将输出指向flare2,flare3,flare4,......的值,直到达到限制。
What I get as the output is the following: 我得到的输出如下:

lens_pos1=¨Camera::Table[Id]->sensor->flare1¨ 
lens_pos2=¨Camera::Table[Id]->sensor->flare2¨ .......  

How can I output the value of flare1 instead of pasting the string? 如何输出flare1的值而不是粘贴字符串?

What I want is the following: 我想要的是以下内容:

lens_pos1=¨10.3¨ lens_pos2=¨12.4¨..... 

Where the values 10.3, 12.4 would be those of flare1 and flare2 respectively taken from a seperate C file. 其中值10.3,12.4将分别取自单独的C文件中的flare1和flare2。

for(int i = 1; i <= nbflares; i++)  
{  
    char integer_string[32];  
    sprintf(integer_string, "%d", i);  
    char flare[100] = "Camera::Table[Id]->sensor->flare";

    strcat(flare,integer_string);

    fprintf(yyout, "lens_pos%d=\"%s\" ",i,flare);
}

You can't access a variable like that in C/C++. 您无法在C / C ++中访问类似的变量。 You have to redesign the "sensor" structure to contain an array instead of individual flares, and access the array by index: Camera::Table[Id]->sensor->flare[1], Camera::Table[Id]->sensor->flare[2], etc. 您必须重新设计“传感器”结构以包含数组而不是单个耀斑,并通过索引访问数组:Camera :: Table [Id] - > sensor-> flare [1],Camera :: Table [Id] - > sensor-> flare [2]等

That can't be done in C++. 这不能用C ++完成。 Some interpreted languages might allow such things because the text of the source code still exists while the program is running. 一些解释型语言可能允许这样的事情,因为源代码的文本在程序运行时仍然存在。 But in C++, when you compile a program all the names of classes and variables and such are essentially lost. 但是在C ++中,当你编译程序时,所有类和变量的名称都会丢失。 When it gets to the point of a running executable, the actual machine instructions are just working with offsets and memory addresses. 当它到达正在运行的可执行文件时,实际的机器指令只是处理偏移量和内存地址。

So you need re-design how the data is stored. 因此,您需要重新设计数据的存储方式。

Is there a way to access them without the use of arrays? 有没有办法在不使用数组的情况下访问它们?

Technically, yes. 从技术上讲,是的。 But only by using a more complicated scheme that would involve a more complicated data structure (such as a linked list or map). 但只能使用更复杂的方案来处理更复杂的数据结构(例如链表或地图)。

Why would you want to avoid arrays anyway? 你为什么要避免使用数组呢? Right now you have a series of variables of the same type that you want to distinguish by the number are the end of their names. 现在,您有一系列相同类型的变量,您希望通过数字来区分它们的名称末尾。 And an array is a series of variables of the same type that are distinguished by their index in the array. 数组是一系列相同类型的变量,它们通过数组中的索引进行区分。 It's pretty much a perfect match. 这几乎是完美的搭配。

For example, if you had a flares array, then you could simply do something like: 例如,如果你有一个flares阵列,那么你可以简单地做一些事情:

for(int i = 0; i < nbflares; i++)  
{  
    fprintf(yyout, "lens_pos%d=\"%f\" ", i, Camera::Table[Id]->sensor->flares[i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM