小编chq*_*lie的帖子

传递给函数时,C类型从char*转换为int

我正在尝试将文件名传递给线程函数,但它的类型在函数内转换为int

struct Data {
    char file_name;
}
void *Requestor(void *args) {
    struct Data *data = (struct Data*)args;
    printf("%s\n", data->file_name); //says expected char* but argument is type of int
}

int file_count = 5;
struct Data files[file_count];

for (int i = 0; i < file_count; i++) {
    printf("%s\n", argv[5 + i]); //this prints the file_name correctly;
    files[i].file_name = argv[5 + i]; // I get: warning: assignment makes integer from pointer without a cast [-Wint-conversion when compiling
    int thread = pthread_create(&(requesterThreads[i]), NULL, …
Run Code Online (Sandbox Code Playgroud)

c

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

Ampersand in PlaceHolder of scanf?

以下代码的输出是什么?

#include <stdio.h>

int n;

int main() {
    scanf("&d", &n);

    switch (n) {
      case 1:
        printf("hello\n");
        break;
      case 2:
        printf("good\n");
        break;
      default:
        printf("Morning\n");
        break;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

大多数人都希望有一个错误,但在执行时,Morning无论给出什么输入,它总是输出.

c scanf

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

将两个指针字符串连接在一起的函数

我在将这两个指针串连接在一起时遇到问题,下面是我的连接函数,我应该将字符串1添加到字符串2.此外,我不能在字符串库中使用任何函数,这就是这是为了通过自己编写代码来帮助我们理解函数中的代码.

char strconcat(char *user2p, char *user1p) {
    while (*user2p) {
        user2p++;
    }
    while (*user1p) {
        *user2p = *user1p;
        *user2p++;
        *user1p++;
    }
    *user2p = '\0';

    printf("test: %c", *user2p);

    return *user2p;
}
Run Code Online (Sandbox Code Playgroud)

这是我的主要部分与功能相关.

int main() {
    char userString1[21], userString2[21];
    char *user1p, *user2p;

    user1p = userString1;
    user2p = userString2;

    printf("Please enter the first string: ");
    gets(userString1);

    printf("Please enter the second string: ");
    gets(userString2);

    printf("String 1 after concatenation: ");
    puts(userString1);
    printf("String 2 after concatenation: %c\n", strconcat(user2p, user1p));
Run Code Online (Sandbox Code Playgroud)

终端一直给我这个,我没有包括长度和字母顺序的代码.当我尝试printf在函数中运行测试时它给了我一个null,当我返回函数时它没有给我任何东西.我很茫然,非常感谢任何帮助!

 Please enter the first …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

无法理解什么是fflush()函数

我似乎无法理解fflush()C语言中函数的概念。有人可以用更简单的术语来解释它,因为我似乎无法理解它及其在代码中的作用:

int main() {
    loadContactList();
    while (1) {
        printf("\n");
        printMenu();

        int choice;

        scanf(" %d", &choice);
        fflush(stdin);
        printf("\n");

        if (choice == 1) {
           // addContact();
        } else if (choice == 2) {

        } else if (choice == 3) {

        } else if (choice == 4) {
            query();
        } else if (choice == 5) {
            while (1) {
                printf("choose the sorting mode:\n \n");
                printf("1. Sort by last name, first name then number\n");
                printf("2. Sort by date\n");
                printf("Enter -1 to return …
Run Code Online (Sandbox Code Playgroud)

c

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

我在运行程序时遇到分段错误(核心转储)

pos.dat 文件包含:

1 2 4
1 2 3
1 2 1
1 2 3
Run Code Online (Sandbox Code Playgroud)

segmentation fault当我运行程序时,我得到了一个.

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

int main() {
    FILE  *fp;
    int line = 4, i = 1;
    float *x, *y, *z;
    fp = fopen("pos.dat", "r");
    while (i <= line) {
        fscanf(fp, "%f%f%f", &*x, &*y, &*z);
        printf(fp, "%f\t%f\t%f\n", *x, *y, *z);
        i = i + 1;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

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

我不明白为什么我肯定会丢失内存块

作为一个项目,我正在创建一个学生数据库。但是根据valgrind,我的程序中存在内存泄漏,我不知道为什么。我真的不能说太多:我不明白为什么记忆肯定会丢失。

学生结构:

typedef struct {
    char student_number[7];
    char *first_name;
    char *last_name;
    int round_points[6];
} Student;
Run Code Online (Sandbox Code Playgroud)

免责声明:我正在使用gcc选项-std=c99,因此必须实现自己的strdup()

重要的代码段:

char *copy_string(const char *string) {
    int len = strlen(string);
    char *copy = calloc(len + 1, sizeof(char));
    if (copy == NULL)
        return NULL;
    strcpy(copy, string);
    /* copy[len] = '\0'; */
    return copy;
}

char **parse_one_line_params(const char *one_line, int param_count) {
    char *copy = copy_string(one_line);
    if (copy == NULL)
        return NULL;
    //copy_start is used to free the copy string in the …
Run Code Online (Sandbox Code Playgroud)

c valgrind memory-leaks memory-management

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

无法使用 scanf 打印商

我在本练习中的目标是创建一个函数名称MultiTwo(),该函数名称将用户插入的两个整数相乘,然后打印商。MultiTwo必须在里面调用main()

这是我的尝试:

#include <stdio.h>

int MultiTwo(int x, int y, int result);

int main() {
    int x, y, result;

    printf("Insert an integer: \n");
    x = scanf("%d", &x);
    printf("Insert a second integer: \n");
    y = scanf("%d", &y);
    result = x * y;

    printf("The quotient of the two inserted integers is: %d", result);

    return 0;   
}
Run Code Online (Sandbox Code Playgroud)

我插入两个整数,尽管插入了整数,但我总是得到的结果是 1。

c

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

指针声明和定义如何为指针工作?

对于以下代码spinet:

#include <stdio.h>

int main(void) {
    int x = 12345678;
    char *a = x;

    printf("%d\n", x);

    printf("%d\n %d\n %d\n %d\n", a[0], a[1], a[2], a[3]);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GNU GCC v7.1.1 的输出:超时:监控的命令转储核心 sh:第 1 行:11792 分段故障超时 10 秒主要

请注意以下工作:

#include <stdio.h>

int main(void) {
    int x = 12345678;
    char *a;
    *a = x;

    printf("%d\n", x);

    printf("%d\n %d\n %d\n %d\n", a[0], a[1], a[2], a[3]);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pointers

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

Can the qsort comparison function always return a non-zero value?

An ascending sort callback function for qsort and bsearch on an array of int could look like this:

int ascending(const void *o1, const void *o2) {
    int a = *(const int *)o1;
    int b = *(const int *)o2;
    return a < b ? -1 : 1;
}
Run Code Online (Sandbox Code Playgroud)

Yet this function seems to violate the constraint on the compar function as specified in the C Standard:

7.22.5.2 The qsort function

Synopsis

#include <stdlib.h>
void qsort(void *base, size_t nmemb, size_t size,
           int …
Run Code Online (Sandbox Code Playgroud)

c sorting qsort language-lawyer

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

C 程序不打印作为输入给出的字符串

我希望输出打印我们打印的数据。但它没有按预期工作,输出没有显示并且正在退出

#include <stdio.h>
    
int main() {
    char name[20], department[3], section[1];
    
    printf("enter the name of the student:");
    scanf("%s", name);
    
    printf("enter your department:");
    scanf("%s", department);
    
    printf("enter the section");
    scanf("%s", section);
    
    printf("Name:%s \n  Department:%s \n  Section: %s  ", name, department, section);
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

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