【问题标题】:contactenating two arrays in c using pointers and function使用指针和函数在c中联系两个数组
【发布时间】:2021-04-30 10:50:52
【问题描述】:
int* concat_tab(int n1,int t1[],int n2, int t2[]){
    int *t3;
    t3 = (int*) malloc((n1+n2)*sizeof(int));
    for (int i = 0; i < n1; i++){
            *(t3+i)=*(t1+i);}
    for (int j = 0; j < n2; j++){
            *(t3+n1+j)=*(t2+j);}
    
    for (int i = 0; i < n1+n2; i++){
        printf("%d\t",*(t3+i));}
}

我正在尝试在两个数组中使用此函数,但出现分段错误。任何帮助!

【问题讨论】:

  • 你的函数被声明为返回一个int*,但它没有。在末尾添加return t3;
  • 向我们展示主要内容。
  • OT: (int*) malloc -> malloc,不需要演员表。
  • 函数正确运行,但没有像 Adrian 上面所说的那样返回 t3! (在使用了t3的返回值后,例如xx=concat_tab在main中,记得释放它(free(xx);

标签: c function pointers function-pointers


【解决方案1】:

您的函数没有return t3; 语句。

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2015-02-07
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多