[英]Passing an int pointer into a function to print that array in C
This is my first post so bear with me. 这是我的第一篇文章,所以请多多包涵。 I am getting back into writing in C after being out of school for a while.
放学一段时间后,我重新开始用C编写代码。 I am trying to pass an array of integers into a function that will print the entire array out on the same line (as a part of a larger effort).
我正在尝试将整数数组传递给一个函数,该函数将在同一行上打印出整个数组(这是更大努力的一部分)。 My dumbed down code follows:
我愚蠢的代码如下:
// main.c
#include <stdio.h>
int main(void) {
int arr[] = {3,4,5,6,7};
int *ptr = arr;
int l = 5;
printarr(arr,l);
return 0;
}
And 和
// function printarr
#include <stdio.h>
void printarr(char *ptr, int l) {
int k = 0;
for (k = 0; k < l; ++k) {
printf("%d", ptr[k]);
}
}
After compiling and running main.c, the output I get is: 编译并运行main.c之后,我得到的输出是:
30004
I feel like I have been beating my head against a wall trying to figure out a simple problem. 我觉得我一直在想办法解决一个简单的问题。 When I iterate and print the array in main I get the output I would expect ('34567') but when I try to do it through the function it comes out sad.
当我遍历并打印main中的数组时,我会得到期望的输出('34567'),但是当我尝试通过该函数执行该操作时,结果就很可悲。 I suspect I am misusing the pointer but yeah I'm not sure.
我怀疑我在滥用指针,但是是的,我不确定。
Help is appreciated. 感谢帮助。
Thanks. 谢谢。
edit: Upon glancing at the first two comments, I am entirely ashamed of myself. 编辑:看了前两个评论,我完全为自己感到ham愧。 Thank you folks.
谢谢大家。 Wow, just wow.
哇,哇
You should change your function to 您应该将功能更改为
void printarr(int *ptr, int l)
as you're passing an int *
to it. 当您将
int *
传递给它时。 As pointed by @Malife, you should enable the compiler warnings with -Wall
, so you get a proper message. 正如@Malife指出的那样,您应该使用
-Wall
启用编译器警告,以便获得正确的消息。 Optionally make all warnings into errors with -Werror
. (可选)使用
-Werror
将所有警告变为错误。
char
and int
have different sizes, int
being much bigger. char
和int
大小不同, int
更大。 4 times bigger as you can see from your output. 从输出中可以看到,它大了4倍。 You're iterating the memory where your
int
numbers are allocated in a partial manner when looking at the values as char
. 当将值视为
char
时,您正在迭代内存,其中部分分配了int
数字。
printarr should be expecting an int array, not char array, so should be printarr应该期待一个int数组,而不是char数组,所以应该
void printarr(int *ptr, int l)
In main, int *ptr = arr; 在主要方面,int * ptr = arr; is not used or needed.
不使用或不需要。
You probally also want to put a space or new line between each value of the array you are printing out: 您可能还想在要打印的数组的每个值之间添加空格或换行符:
void printarr(char *ptr, int l) {
int k = 0;
for (k = 0; k < l; ++k) {
printf("%d\n", ptr[k]);
}
}
I think the problem is that you pass a char pointer in "printarr". 我认为问题是您在“ printarr”中传递了一个char指针。 In fact when you pass "ptr" you have this situation: 00000011 00000000 00000000 00000000 00000100 .... A char has 8 bit, an int 32 bit, and the Ram is often Little Endian for the bytes order.
实际上,当您传递“ ptr”时,您会遇到以下情况:00000011 00000000 00000000 00000000 00000100 ....一个char有8位,一个int 32位,Ram通常是Little Endian,用于字节顺序。 Then you call a printf passing an int, and the byte is extended to 32 bits so you print: 3 0 0 0 4. In the main "ptr" is an int, so you print the int, in this case 3 4 5 6 7. You must change char *ptr -> int *ptr in "printarr"
然后,您调用通过int的printf,并将字节扩展为32位,以便进行打印:3 0 0 0 4.在主“ ptr”中是int,因此您将int打印,在这种情况下为3 4 5 6 7.您必须在“ printarr”中更改char * ptr-> int * ptr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.