I am attempting to take a input file up to 2048 bytes and place it in its own array in layer4. I am also attempting to place the size of the array in spot [0] of the array for latter use. 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. Currently when I compare the address of my pointer in layer4 and layer3 to each other, they match. However when i check for values in the array in layer3, they do not match the values of the array in my input file. 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
^