繁体   English   中英

我收到错误:“数组下标无效的类型'long int [int]”

[英]I get the error: “invalid types 'long int[int] for array subscript”

在计算机科学领域,我们进行了一些练习来学习c编码。 这些练习之一是创建一个函数copyField,该函数具有3个参数:targetField sourceField和字段的Length。

我们应该创建一个使用sourceField(必须具有长值)并将其复制到targetField的函数。

我的代码:

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

//declaration of the function
long copyField(long targetField, long sourceField, int length);

int main(void){

  //sField will be variable afterwards
  /*sField equals sourceField; tField equals targetField; 
  len equals Length of sField 
  */

  long sField[]={12341234,2343233,3432424,4235252354,53254234};
  long tField[sizeof(tField)/sizeof(int)];
  int len=sizeof(sField)/sizeof(int);

 /*commented because I have to fix the error first
  copyField(tField,sField,len);
 */

 //print out the targetField

  for(int i=0;i<=sizeof(tField)/sizeof(int);i++){
    printf("%d",sField[i]);
  }

  return(0);

}

//function
long copyField(long a,long  b,int c){

  //for i < length of sField 

  for(int i=0;i<c;i++){
    /*targetField should be filled with the content
    of sourceField
    */
    a[i]=b[i];
  }

}

您能帮我解决这个问题吗? 我有点陌生,现在才几周,所以希望您能帮我...

该函数的targetFieldsourceField参数不是数组,您将以数组形式访问它们。 而且long不是未签名的,您可能为其分配了太大的值。

尝试这个

long copyField(long targetField[], long sourceField[], int length);

暂无
暂无

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

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