在C / C ++中将指针(数组的名称)传递给函数(passing pointers (the name of array) into function in C/C++)
比方说,我们创建一个数组,如:
int a[4]={1,2,3,4};
现在,
a
是该数组的名称,并且指针指向第一个元素a[0]
。 所以当我想调用数组中的元素时,我可以使用a[ i ]
或*(a+i)
。现在我有一个功能:
void print_array(int* array, int arraySize){ for(int i=0; i<arraySize; i++){ cout<<*(array+i)<<endl; cout<<array[i]<<endl; } }
当我使用
print_array(a,4)
a[4]={1,2,3,4}
传递a[4]={1,2,3,4}
此函数时,对于cout
的第一行,我完全理解,因为我使用*(a+i)
方法访问数据和a
是我通过的指针。我无法理解的是:因为我将一个指针传递给函数,为什么我可以使用
a[i]
的格式,比如cout
的第二行? 不是一个指针? 如果a
是一个指针,为什么a[i]
工作?这让我困惑了整整一天。 任何帮助都感激不尽!
Lets say we create an array like:
int a[4]={1,2,3,4};
Now
a
is the name of this array and also the pointer points to the first elementa[0]
. So when I want to call the elements in the array, I can usea[ i ]
or*(a+i)
.Now I have a function:
void print_array(int* array, int arraySize){ for(int i=0; i<arraySize; i++){ cout<<*(array+i)<<endl; cout<<array[i]<<endl; } }
When I pass
a[4]={1,2,3,4}
into this function usingprint_array(a,4)
, for the first line ofcout
, I fully understand because I use*(a+i)
method to access data anda
is the pointer I passed.What I can't understand is: since I pass a pointer
a
into function, why can I usea
in the format ofa[i]
like the second line ofcout
? Isn'ta
a pointer? Ifa
is a pointer why doesa[i]
work?This has confused me for a whole day. Any help will be much appreciated!
原文:https://stackoverflow.com/questions/42203474
最满意答案
d3.csvParse
返回一个数组,但是你在这里将它转换为一个字符串:
var data = JSON.stringify(d3.csvParse(data));
当然,字符串没有
.map
。 你可能想遍历数组:const newData = d3.csvParse(data); newData.forEach((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`));
(不要使用
.map
因为你没有把它转换成一个新的数组,至少不是你发布的代码)
d3.csvParse
returns an array, but you convert it to a string here:
var data = JSON.stringify(d3.csvParse(data));
Strings don't have
.map
, of course. You probably want to iterate over the array:const newData = d3.csvParse(data); newData.forEach((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`));
(don't use
.map
because you aren't transforming it into a new array, at least not in the code you've posted)
相关问答
更多-
Text::CSV的getline方法返回一个arrayref 它使用$ io-> getline()从IO对象$ io中读取一行,并将此行解析为数组ref。 ARRAY(0x35e9360)确实是打印出数组引用时得到的。 通常,许多解析器通常返回对行的数组的引用。 因此,您需要取消引用,通常是@{$arrayref} ,但在这种情况下,没有歧义,可以删除@$arrayref , @$arrayref 。 use warnings; use strict; use Text::CSV_XS; use XML ...
-
迭代python中的特定csv行输出一个空白文件(Iterating over specific csv rows in python outputs a blank file)[2023-07-13]
有罪的一方是 row_count = sum(1 for row in reader2) 它读取来自reader2所有数据; 现在,当您尝试for row in islice(reader2, 7, last_line_index)您不会获得任何数据。 此外,您可能正在阅读大量空白行,因为您将文件打开为二进制文件; 相反 with open('file.csv', newline='') as inf: rd = csv.reader(inf) The guilty party is row_c ... -
简单的解决方案是添加变量is_found作为示例: is_found = False for row in contents: print('begin loop') if username == row[4]: print("Username found") if password == row[3]: print("Password found") main() is_found = ...
-
你可以检查$data数组的大小。 就像是: while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if(count($data) == 4){ $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) " . "values($wkey, '$data[0]','$data[1]',' ...
-
map函数实际上返回一个可迭代的函数结果。 所以你只需要返回df : def readcsv(file): df = pd.read_csv(file, delimiter="\s+", names=[headers], comment="#") #DOING SOME OTHER STUFF TO IT return df if __name__ == "__main__": files = "glob2 path to files" with concurr ...
-
通过csv迭代(Iterating through csv)[2022-12-31]
d3.csvParse返回一个数组,但是你在这里将它转换为一个字符串: var data = JSON.stringify(d3.csvParse(data)); 当然,字符串没有.map 。 你可能想遍历数组: const newData = d3.csvParse(data); newData.forEach((item, i) => console.log(`Hello ${item.col1} this ${item.col2} there are ${item.col3} blabla`)); ... -
你可以编写一个生成器,它将返回除输入迭代器中最后一项之外的所有内容: def skip_last(iterator): prev = next(iterator) for item in iterator: yield prev prev = item 然后将您的CSV_raw阅读器对象包装CSV_raw : for row in skip_last(CSV_raw): 生成器基本上接受第一个条目,然后开始循环,并在每次迭代时产生先前的条目。 当输入迭代器 ...
-
正如@ amal-ts所说,你应该使用append模式来打开文件。 在每个循环中打开文件都是个坏主意,所以你应该考虑使用 with open("output.csv", "ab") as f: for ind1,key1 in enumerate(file1): for ind2,key2 in enumerate(file2): ques1 = file1[key1] ques2 = file2[key2] m ...
-
对两个循环使用相同的变量x 。 在第二个循环中,无论客户是什么, x都会从0到12,并且由于您只用x设置了行号,所以您停留在第一个客户上。 你的双循环应该看起来像这样: # loop over the customers for x_customer in range(0,len(overalldata),12): # loop over the months for x_month in range(0,12,1): # line number: x x = ...
-
通常,Web应用程序使用Cookie来区分经过身份验证的用户以及未经身份验证的用户 因此,通过将HTTP Cookie管理器添加到您的测试计划并选中“每次迭代清除cookie”复选框,您将能够模拟此LogIn - > Logout行为,并且能够缩小所需的唯一用户名/密码组合的数量。 请记住, 迭代是在线程组级别上定义的,由触发器控制器将不会清除Cookie管理器中的cookie。 Usually web applications use Cookies to distinguish authenticate ...