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

时间:2015-04-15 19:42:29

标签: c arrays pointers data-structures struct

我试图获取最多2048字节的输入文件,并将其放在layer4中自己的数组中。我也试图将数组的大小放在数组的spot [0]中供以后使用。当layer4完成时,我试图将指向名为code的数组的指针传递给transmit函数,它将该值传递给layer3并将数组放入结构中。目前,当我将layer4和layer3中指针的地址相互比较时,它们匹配。但是,当我在layer3中检查数组中的值时,它们与我的输入文件中的数组值不匹配。此代码将成为更大项目的一部分。我收到的各种警告都在我的代码底部:

#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; 


};
}

输出

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   

警告

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
 ^

1 个答案:

答案 0 :(得分:1)

你在整个地方混合指针和整数。这并不总是会导致问题,但最好是不好的做法。对于您的具体问题,原因可能是:

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;

将int指针传递给layer3调用。但是你把它的地址放在了layer3里面。那不是你想要的。同样,整体和指针的混合不是这里的根本原因,但确实导致了混乱。你的代码应该是这样的:

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

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

也就是说,不要更改指向整数的指针。只需将指针直接传递(特别是进入layer3)。编译器警告已经强烈暗示了这一点。如果你摆脱了所有这些警告,那么你走在正确轨道上的一个好兆头。

还有一些提示:

  1. 您没有为您的函数声明显式返回类型。明确地声明这一点总是很好的做法。
  2. 正确格式化代码(一致的间距等)。特别是在Stackoverflow上发布时,但对自己来说更是如此。