小编chq*_*lie的帖子

打印地址时出现意外的值

这是我的计划.输出如下.

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

int main(void) {
    int n = 3, *pt;
    pt = &n;
    printf("\n%d", pt);
    printf("\n%d", &n);
    printf("\n%d", &pt);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

-9705676
-9705676
-9705672
Run Code Online (Sandbox Code Playgroud)

但我认为第三个数字应该与前两个相同?请有人解释一下为什么不是这样?

c pointers

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

我执行二分查找的代码哪里出错了?

我正在为二分搜索算法编写代码。

代码:

#include "cs50.h"

int main(void) {
    int n = GetInt();
    int value = GetInt();
    int values[n];

    for (int i = 0; i < n; i++) {
        printf("Put in number %i ", i + 1);
        values[i] = GetInt();
    }

    int mid = (n - 1) / 2;
    int en = 0;
    int ex = n - 1;

    for (int i = 0, xt = i + 1; i < xt; i++) {
        if (value > values[mid]) {
            en = mid; …
Run Code Online (Sandbox Code Playgroud)

c algorithm binary-search cs50

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

以标准格式打印名称

我正在尝试开发一个基本程序,它以您的名字命名并以标准格式提供输出.问题是我希望用户可以选择不添加中间名.

例如:Carl Mia Austin给了我CM Austin但是我想要即使输入是Carl Austin它也应该给C. Austin而不询问用户他们是否有中间名.那么,有没有一种方法或功能可以自动检测到?

#include <stdio.h>

int main(void) {
    char first[32], middle[20], last[20];

    printf("Enter full name: ");
    scanf("%s %s %s", first, middle, last);
    printf("Standard name: ");
    printf("%c. %c. %s\n", first[0], middle[0], last);

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

c string

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

将用户输入字符串写入C中的文件

我的程序需要获取用户的输入并将其保存到外部文件以供将来参考.这是代码的基本概要.

void newActivity(FILE *foutput) {
    char name[31];
    char description[141];
    finput = fopen("activities.txt", "a");

    printf("\n What is the name of your activity (up to 30 characters):\n");
    fgets(name, sizeof(name), stdin);

    printf("\nEnter a brief description (up to 140 characters) of what %s is about:\n",
           fputs(name, stdout));
    fgets(description, sizeof(description), stdin);

    if (finput == NULL) {
        printf("\nCould not open file.");
        exit(1);
    }

    fprintf(foutfile, "%s\n", name);
    fprintf(foutfile, "%s\n", description);

    fclose(foutfile)
}
Run Code Online (Sandbox Code Playgroud)

当我运行一个只询问名称并打印该名称的简单测试程序时,一切都很好.它看起来像这样:

int main() {
    char name[50];
    fprint("What is your name? ");
    fgets(name, sizeof(name), stdin);
    fputs(name, …
Run Code Online (Sandbox Code Playgroud)

c io file-io

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

C - 读取整个文件最后带有垃圾

所以我试图使用这个函数读取整个文本文件:

FILE *fp = fopen(path, "r");
fseek(fp, 0, SEEK_END);
int tamanioArchivo = sizeof(char) * ftell(fp);
fseek(fp, 0, SEEK_SET);
char* archivo = malloc(tamanioArchivo + 1);
fread(archivo, tamanioArchivo + 1, 1, fp);
//do something with archivo
fclose(fp);
free(archivo);
Run Code Online (Sandbox Code Playgroud)

我调试了它,问题似乎就fread行了。它带回文件并在最后添加一些垃圾。任何想法我做错了什么?

c file

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

如何在printf语句中创建条件?

我有功课,我必须创建一个C程序,从最小到最大排序5个数字.我知道如何使用函数和if语句(使用>=<=)轻松编程.

然而,美中不足的是,我只允许使用printfscanf,所以我必须要计算所有的>=<=内部的printf.我不允许使用三元运算符.

我一直在努力.所以我试着只排序3个数字,我仍然无法通过排序最小的数字.它只是保持打印1.

// This printf is just trying to figure out the smallest number from 3 numbers provided but it keeps printing 1.
printf("The ascending order of the numbers are: %d ",
    (
     (num1 <= num2 && num1 <= num3) || // Checking if num1 is the smallest
     (num2 <= num1 && num2 <= num3) || // Checking if num2 is …
Run Code Online (Sandbox Code Playgroud)

c printf conditional if-statement

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

为什么此C程序的输出是64,而不是320?

#include <stdio.h>

int main() {
    int a = 320;
    char *ptr;

    ptr = (char *)&a;
    printf("%d", *ptr);

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

我期望输出是,320但是我得到了64。这是什么原因呢?

c pointers dereference

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

如何使2D字符数组完全为空?

我正在使用malloc创建一个2D数组,当我遍历该数组时,有一些我不想要的字符和符号。数组不应该完全为空吗?

我试图将空字符分配给每一行的开头,但这不会改变任何内容。

    char **structure;
    structure = malloc(sizeof *structure * 2);
    if (structure) {
        for (size_t i = 0; i < 2; i++) {
            structure[i] = malloc(sizeof *structure[i] * 20);
            structure[i][0] = '\0';
        }
    }
    for (int i = 0; i <= 2; i++) {
        for (int j = 0; j < 20; j++) {
            printf("%c ", structure[i][j]);
        }
        printf("\n");
    }

Run Code Online (Sandbox Code Playgroud)

我期望输出只是空白,但这是出现的情况:

Z Ñ             P  Ñ             l   L O
Z Ñ               P  Ñ             N U M B
Run Code Online (Sandbox Code Playgroud)

c

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

C 中结构的通用排序函数

所以我有这个问题。我应该创建一些排序函数以在 C 中的通用排序例程中使用。我只有一个函数可以工作。该函数应该用作结构的排序函数。代码应按年份排列列表。

以下是预先编写并用于排序例程的两个辅助函数的代码:

static
void swap(void **left, void **right) {
    void *temp = *left;
    *left = *right;
    *right = temp;
}

void sort_array(void *Array[], unsigned size, int (*ordered)(void *, void *))
{
    int i;
    int have_swapped = 1;

    while (have_swapped) {
        have_swapped = 0;
        for (i = 0; i < size - 1; ++i ){
            if (ordered(Array[i], Array[i+1])) {
                swap(&Array[i], &Array[i+1]);
                have_swapped = 1;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后就是这个函数,也是预写在 main 中用来测试的。

int main() {
    int i;
    int status = …
Run Code Online (Sandbox Code Playgroud)

c pointers structure function-pointers

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

为什么for循环没有结束?

我编写了一个程序,将变量定义npint并使用 对其进行初始化getchar。但这样程序就不会退出for循环。如果我使用scanf它运行良好。为什么会发生这种情况?

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

typedef struct parents {
    char fathername[30];
    char Mothername[50];
} parents;

struct person {
    char firstname[30];
    char Lastname[30];
    int age;
    parents p;
};

int main() {
    int np;
    printf("The number of people: ");
    np = getchar(); //scanf("%d", &np);

    struct person *point = malloc(np * sizeof(struct person));

    int i = 0;
   
    for (i = 0; i < np; i++) {
        printf("Give your name: ");
        scanf("%s", &point[i].firstname); …
Run Code Online (Sandbox Code Playgroud)

c

3
推荐指数
2
解决办法
213
查看次数