- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试获取一个最多 2048 字节的输入文件并将其放置在第 4 层中它自己的数组中。我还试图将数组的大小放在数组的 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
^
最佳答案
你到处都是指针和整数。这并不总是会导致问题,但充其量是一种不好的做法。对于您的具体问题,原因可能是这样的:
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 里面。那不是你想要的。同样,混合使用 int 和指针并不是这里的根本原因,但确实造成了困惑。你的代码应该是这样的:
transmit(int* getdata){ //gets pointer value
int g = layer3(getdata); //puts pointer into new function
}
layer3(int *b){
int *w = b;
也就是说,不要将指针更改为整数。只需将指针直接传递(特别是传递到第 3 层)。编译器警告已经向您强烈暗示了这一点。如果您摆脱了所有这些警告,那么表明您走在正确轨道上的一个好兆头。
更多提示:
关于c - 将指向数组的指针传递给另一个函数 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659392/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!