简体   繁体   English

将指向数组的指针传递给另一个函数C

[英]Passing a pointer to an array to another function C

I am attempting to take a input file up to 2048 bytes and place it in its own array in layer4. 我正在尝试获取一个最大2048字节的输入文件,并将其放置在layer4中自己的数组中。 I am also attempting to place the size of the array in spot [0] of the array for latter use. 我也试图将数组的大小放在数组的点[0]中,以备后用。 When layer4 finishes I am attempting to pass the pointer pointing to the array called code to transmit function where it will pass that value to layer3 and place the array in a structure. 当layer4完成时,我尝试传递指向称为code的数组的指针以transmit函数,该函数会将该值传递给layer3并将数组放置在结构中。 Currently when I compare the address of my pointer in layer4 and layer3 to each other, they match. 当前,当我将第4层和第3层中的指针地址相互比较时,它们匹配。 However when i check for values in the array in layer3, they do not match the values of the array in my input file. 但是,当我在layer3中检查数组中的值时,它们与输入文件中数组的值不匹配。 This code is to be part of a bigger project. 该代码将成为更大项目的一部分。 The various warnings I receive are at the bottom under my code: 我收到的各种警告位于代码的底部:

#include <stdio.h>   
#include <stdlib.h>


main()
{
int *senddata;
senddata = layer4(); // get pointer address of input array
transmit(senddata); //put pointer value into transmit


}

int layer4(){
    FILE *file = fopen("sendtext.txt", "r"); //
    char *code;
    size_t n = 0;
    int c;
    if (file == NULL)
        return NULL; //could not open file

    code = malloc(2048); //allocate memory 

    while ((c = fgetc(file)) != EOF)
    {
    n++;       
    code[n] = (char) c;
    printf("%c", code[n]);
    }
    code[n] = '\0';
    n = n-1;  // for some reason the byte size is +1 for what it should be
    code[0] = n;  

  printf("Check Pointer Address in layer 4: %p \n", code); //test to see pointer address
  printf("Check to see value in pointer:%c \n", code[0]); //check to see if the byte size was placed in the array
  printf("Byte size:%zd\n", n); /// see array size


return code;
}

transmit(int* getdata){  //gets pointer value
int newdata = getdata;
int g = layer3(newdata); //puts pointer into new function
}

layer3(int b){
int x = b;
int w = &x;
char *MSS;
MSS = malloc(60); 


printf("Check to see value in pointer:%c \n", w);


printf("Check Pointer Address in layer 3:%p \n", x); //test to see pointer address

struct l3hdr {  
  char ver;
  char src_ipaddr[16];
  char dest_ipaddr[16];
  char reserved[7];
};

struct l3pdu {
// put array here
struct l3hdr hdr3; 


};
}

Output 产量

Q sadfasd fsa asd fsadf sad f /// This is my input testfile
Check Pointer Address in layer 4: 0xa81250 
Check to see Byte size in array: 
Check to see first input character in array:Q 
Byte size:29
Check to see value in pointer:P 
Check Pointer Address in layer 3:0xa81250   

Warnings 警告

lab.c:20:9: warning: return makes integer from pointer without a cast [enabled by default]
         return NULL; //could not open file
         ^
lab.c:37:2: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
  printf("Check to see value in pointer:%c \n", code);
  ^
lab.c:43:1: warning: return makes integer from pointer without a cast [enabled by default]
 return code;
 ^


lab.c: In function ‘transmit’:
lab.c:47:15: warning: initialization makes integer from pointer without a cast [enabled by default]
 int newdata = getdata;
               ^


lab.c: In function ‘layer3’:



lab.c:64:9: warning: initialization makes integer from pointer without a cast [enabled by default]
 int w = &x;
         ^


lab.c:72:1: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’ [-Wformat=]
 printf("Check Pointer Address in layer 3:%p \n", x); //test to see pointer address
 ^

You are mixing pointers and integers all over the place. 您正在各处混合使用指针和整数。 That doesn't always cause problems but is bad practice at best. 这并不总是会引起问题,但充其量是不好的做法。 For your specific problem, the cause is likely to be this: 对于您的特定问题,原因可能是:

transmit(int* getdata){  //gets pointer value
    int newdata = getdata;
    int g = layer3(newdata); //puts pointer into new function
}

layer3(int b){
    int x = b;
    int w = &x;

You pass an int pointer to the layer3 call. 您将一个int指针传递给layer3调用。 But then you take it's address inside layer3. 但是随后您将其地址放在layer3中。 That's not what you want. 那不是你想要的。 Again, the mixing up of ints and pointers isn't the root cause here but does contribute to the confusion. 同样,整数和指针的混合并不是这里的根本原因,但确实会造成混乱。 Your code should be something like this: 您的代码应如下所示:

transmit(int* getdata){  //gets pointer value
    int g = layer3(getdata); //puts pointer into new function
}

layer3(int *b){
    int *w = b;

That is, don't change pointers to integers. 也就是说,不要将指针更改为整数。 Just pass the pointer directly around (specifically into layer3). 只需将指针直接传递(特别是传递到layer3中)即可。 The compiler warnings already hint this to your strongly. 编译器警告已强烈提示您。 A good sign that you are on the right track is if you get rid of all those warnings. 如果您摆脱了所有这些警告,则表明您处于正确的轨道。

A few more tips: 其他提示:

  1. You don't have explicit return types declared for your functions. 您没有为函数声明显式的返回类型。 Always good practice to declare that explicitly. 始终最好明确声明这一点。
  2. Format your code properly (consistent spacing, etc). 正确设置代码格式(保持一致的间距等)。 Especially when posting on Stackoverflow but really even more so for yourself. 尤其是在Stackoverflow上发布时,但对于您自己而言甚至更多。

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

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