相关疑难解决方法(0)

我们为什么要在C中经常输入一个结构?

我见过许多程序,包括如下所示的结构

typedef struct 
{
    int i;
    char k;
} elem;

elem user;
Run Code Online (Sandbox Code Playgroud)

为什么经常这么需要?任何具体原因或适用范围?

c struct typedef

379
推荐指数
10
解决办法
49万
查看次数

如何使用指针从不同的函数访问局部变量?

我可以在不同的函数中访问局部变量吗?如果是这样,怎么样?

void replaceNumberAndPrint(int array[3]) {
    printf("%i\n", array[1]);
    printf("%i\n", array[1]);
}

int * getArray() {
    int myArray[3] = {4, 65, 23};
    return myArray;
}

int main() {
    replaceNumberAndPrint(getArray());
}
Run Code Online (Sandbox Code Playgroud)

上面一段代码的输出:

65
4202656
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?"4202656"是什么意思?

我是否必须在replaceNumberAndPrint()函数中复制整个数组才能比第一次更多地访问它?

c c++ pointers local-variables

50
推荐指数
3
解决办法
3万
查看次数

C指针示例 - 这有什么不好的?

在我们的讲座中,我们有以下示例:

int *foo(int x) {
    return &x;
}

int* pt = foo(x);
*pt = 17;
Run Code Online (Sandbox Code Playgroud)

它被证明是一个不好的例子,但没有进一步解释.为什么这么糟糕?

c pointers

4
推荐指数
1
解决办法
150
查看次数

创建和返回数组的函数会导致问题

我试着编写一个返回像素颜色的随机数组的函数,所以当我调用时randomPalette(i),该函数将创建一个随机i颜色数组.以下是我的代码.它表示random[colors]该表达式的错误必须具有恒定值.我不知道为什么.怎么解决?

pixel* randomPalette(int colors){

pixel random[colors] ;
int i, x;
srand(time(NULL)); //generate a random seed
for (i = 0; i < colors; i++){
    x = rand() % 256;
    random[i].r = x; random[i].g = x; random[i].b = x;
}
return random;
}
Run Code Online (Sandbox Code Playgroud)

c arrays function variable-length-array

2
推荐指数
1
解决办法
85
查看次数

从C中的函数返回一个局部变量

我在这里找到了一个类似的问题和解决方案 - 从C中的函数返回一个局部变量

但想出了同样问题的另一个解决方案,并想请你评估它,它是否正确?

int myfunction (char **returnval) {
    int isvalue = 0;
    char *d;
    d = "Lorem";
    *returnval = d;
    return isvalue;
}


int main(int argc, char **argv) {
    int func_return;
    char *myvar;

    func_return = myfunction(&myvar);
    printf("myvar=[%s]\n", myvar);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:myvar = [Lorem]

这段代码是否正确?由于功能范围,vars使用的内存不会丢失?

谢谢.

c

1
推荐指数
1
解决办法
245
查看次数

在哪种方式中,字符数组在本地分配内存?

这是我写的代码,

    char *foo();

    void main()
    {
      char *str=foo();
      strcpy(str,"Holy sweet moses! I blew my stack!!");
      printf("%s",str);
    }

    char * foo()
    {
      char str[256];
      return str;
    }
Run Code Online (Sandbox Code Playgroud)

当我在函数foo()中使用char数组时,main()函数中的strcpy不会将字符串复制到str中.但是,当我在函数foo()中使用int数组时,main()strcpy成功复制.

   int str[256]; //in function foo
Run Code Online (Sandbox Code Playgroud)

产量

   Holy sweet moses! I blew my stack!!
Run Code Online (Sandbox Code Playgroud)

如果

   char str[256]; //in foo()
Run Code Online (Sandbox Code Playgroud)

输出:没事!

c

1
推荐指数
1
解决办法
126
查看次数

返回 C 中的局部变量混淆

最近我在 stackoverflow 上读到一个线程,无论是指针类型还是普通变量,都必须避免返回局部变量。我在我的 C 书中看到一个例子,它返回一个局部变量,所以我想再试一次

#include <stdio.h>
int func(int a, int b)
{
    int d, e, f;
    d = a;
    e = b;
    f = d+e;
    return f;
}
int main(void)
{
    int c = func(1, 2);
    printf("hello\n");//I put this printf in between
    printf("c = %d\n", c);//here c should be overwritten
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在该线程中据说,如果我在函数调用和访问该变量之间放置任何内容,我将错过该值。

无论我做什么,我都可以访问局部变量,但我记得我根据该线程编写了一个示例,并且显示了与所告诉的相同的行为。

我缺少什么?

c local-storage

1
推荐指数
1
解决办法
2001
查看次数

当使用c代码动态数组时,不同版本的gcc输出不同

我正在用c语言编写一个有关动态数组的程序,代码是:

#include <stdio.h>

struct Vector {
    int size;
    int capacity;
    int *arr;
};

void add(struct Vector *Arr, int data) {

    if (Arr->size == Arr->capacity) {
        Arr->capacity *= 2;
        int arr[Arr->capacity];

        //array copy
        for (int i = 0; i < Arr->size; i++) {
            arr[i] = Arr->arr[i];
        }

        Arr->arr = arr;
    }

    int size = Arr->size;
    Arr->arr[size] = data;
    Arr->size++;

}

void display(struct Vector *Arr) {
    for (int i = 0; i < Arr->size; i++) {
        printf("%d ", Arr->arr[i]);
    }
    printf("\n");
} …
Run Code Online (Sandbox Code Playgroud)

c dynamic-arrays

1
推荐指数
1
解决办法
71
查看次数

C代码的基本错误

我正在为考试做正确的学习,我遇到了这个问题:

这段代码中的关键错误是什么?

int *numbers(int a, int b)
  {
     int array[2];
     array[0] = a * b;
     array[1] = a + b;
     return array;
  }
Run Code Online (Sandbox Code Playgroud)

现在,我没有太多的C经验,但我根本没有看到这个代码中的严重错误.也许我是一个白痴,只是忽略了一些明显的东西.我唯一能看到的是没有使用malloc分配内存,但我没有看到这是一个大问题.

任何帮助表示赞赏!

c

0
推荐指数
1
解决办法
101
查看次数

指针在这里如何工作?

如果控制返回到main()变量之后,如果从堆栈中删除了i仍然是5,因为我在main()中不存在并且指针指向的变量不是存在.

#include<stdio.h>

int* sum() {
    int i=5;
    int*a=&i;
    printf("%d\n",a);
    return a;
}

int main() {
    int* a=sum();
    printf("%d\n",a);
    printf("%d",*a);
}
Run Code Online (Sandbox Code Playgroud)

输出:

2293252
2293252
5
Run Code Online (Sandbox Code Playgroud)

c pointers

0
推荐指数
1
解决办法
51
查看次数

无法将'char(*)[1024]'转换为'char*'作为回报

我想逐行读取一个简单的文件,并将内容保存到一个数组中.如果我编译源代码,我会收到此错误:

test.C: In function ‘char* read_file(int, int, const char*)’:
test.C:14:11: error: cannot convert ‘char (*)[1024]’ to ‘char*’ in return
    return words;
           ^
Run Code Online (Sandbox Code Playgroud)

这是什么意思?为什么我不能返回2D数组的指针?这是源代码.

#include <stdio.h>
#include <string.h>

char * read_file(int buf, int size, const char *fname){
   char words[size][buf];
   FILE *fptr = NULL; 
   int idx = 0;

   fptr = fopen(fname, "r");
   while(fgets(words[idx], buf, fptr)) {
      words[idx][strlen(words[idx]) - 1] = '\0';
      idx++;
   }
   return words;
}

int main(void) {
   char * words;
   words = read_file(1024, 100, "text.txt");
   int i;
   for(i = 0; …
Run Code Online (Sandbox Code Playgroud)

c readfile

0
推荐指数
1
解决办法
945
查看次数

printf如何更改函数的返回值?

使用printf语句printf("new s3 string (in locate function) is \"%s\" \n",s3),代码可以正常工作

但是当printf("new s3 string (in locate function) is \"%s\" \n",s3)评论时,代码返回null

如何printf影响return价值?

#include <stdio.h>
#include <string.h>

char * locate(char * s1,char * s2,int index) 
{
    printf("s1 string is \"%s\" \n",s1);
    printf("s2 string is \"%s\" \n",s2);

    int i=0,j=0, k;
    char s3[100];    

    while(i<=index)
    {
        s3[i]=s1[i];
        i++;
    }
    k=i;

    while(s2[j]!='\0')
    {
        s3[i]=s2[j];
        i++; j++;
    }

    while(s1[k]!='\0')
    {
        s3[i]=s1[k];
        i++;k++;
    }

    s3[i]='\0';

    //printf("new s3 string (in locate function) …
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
157
查看次数

指针使用指针给出意想不到的结果

为什么印刷第一印刷声明main()11?

#include<stdio.h>
void foo(int ** p){
    int j = 11;
    *p = &j;
    printf("%d ", **p);        //Printing 11
}

int main(){
    int i = 10;
    int *p = &i;
    foo(&p);    
    printf("%d ", *p);          //Printing 11
    printf("%d ", *p);          //Printing Random value
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers

-1
推荐指数
1
解决办法
70
查看次数