相关疑难解决方法(0)

我正在尝试增加C字符数组中的值,并且它会一直失败 - 帮助?

这是我的函数代码:

char * increment_b_string(char * b_string)
{
    char * ret_string = (char *) malloc(7);

    ret_string = "001aaa";

    if (*(b_string + 2) == '9')
    {
        *(ret_string +2) == '0';

        if (*(b_string + 1) == '9')
        {
            *(ret_string +1) = '0';
            *(ret_string) = *(b_string) + 1;
        } else {
            *(ret_string + 1) = *(b_string + 1) + 1;
        }
    } else {
        *(ret_string + 2) = *(b_string + 2) + 1;
    }

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

有关它为什么会失败的任何想法?

一般的想法是b_string将包含类似"123aaa"的值."aaa"部分并不重要,永远不会改变.只有前3个数字会.它们需要增加,就好像它们是整数一样.需要在开始时保留前导零.因此,如果输入为"004aaa",则输出应为"005aaa".这只需要达到"300aaa",因此我不会考虑更多的东西.这是一个学校的编程任务,因此问题非常人为.

谢谢.

编辑:我改变了我的功能.这就是现在的情况.

void increment_b_string(char * …
Run Code Online (Sandbox Code Playgroud)

c

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

C中的Malloc问题

解决方案:在程序退出后释放已分配的内存.必须从磁盘读取+写回链表,然后重写以更新数据库!谢谢大家=)

您好,我基本上已经在这个数据库程序中工作了几天,但我只是不断达到死胡同.这项任务今天到期,所以如果你能帮助我,我将非常感激.= T

数据库使用链接列表实现,由几个文件组成:sdbm.c,sdbm.h,new.c,get.c,insert.c,put.c和remove.c.sdbm.c包含基于sdbm.h接口的数据库方法,其他文件包含使用sdbm方法的主要方法.

第一个问题来自insert程序,当我尝试添加一个键和值对时似乎工作正常......也就是说,直到我们再次尝试调用insert程序.分配的内存似乎已经消失了!我一直在研究,试图弄清楚为什么即使我有malloced,为什么它会在插入程序退出后消失.这是一些代码:

  • 节点结构+全局变量:
struct dbase_Node {
  char *keyValue;
  char *element;
  struct dbase_Node *next;
};

typedef struct dbase_Node Node;

Node *head;
Run Code Online (Sandbox Code Playgroud)

========

  • 插入方法
static bool sdbm_insert_back(Node **headRef, const char *key, const char *value)
{
  Node *new = (Node *)malloc(sizeof(Node));
  if (new == NULL)
    return false;
  else {
    new->keyValue = malloc(strlen(key));
    new->element = malloc(strlen(value));
    strcpy(new->keyValue, key);
    strcpy(new->element, value);

    new->next = *headRef;
    *headRef = new;
    return true;
  }
}
Run Code Online (Sandbox Code Playgroud)
  • 同步方法
bool sdbm_sync()
{
  if …
Run Code Online (Sandbox Code Playgroud)

c database memory malloc pointers

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

如何让它不共享相同的内存

#include <stdio.h>
typedef struct TESTCASE{
    char *before; 
}ts;
int main(void) {
     ts t[2] = {{"abcd"}, 
                {"abcd"}};
     t[0].before[0] = t[0].before[2] = t[0].before[3] = 'b';
     printf("%s - %s\n", t[0].before, t[1].before);
     return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是

bbbb - bbbb

我在Cygwin中用gcc编译

cc -g test.c -o test

我的问题是,使用什么编译选项,我可以收获bbbb - abcd的结果?

c

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

将此 Json 文件的输入转换为 C 结构

我有一个名为 name.json 的 Json 文件,可以在下面看到此类 Json 文件的示例

{
  "set": 5,
  "low": 0,
  "draw_set": "0.1 up to 0.3",
  "Wupet": "Hold",
  "": null
}
Run Code Online (Sandbox Code Playgroud)

但也有可能是另外一次 Json 文件具有另一种结构。

{
  "set": 5,
  "low": 0,
  "draw_set": "0.1 up to 0.3",
  "W_set": "Ramp 1.5 ?C/min",
  "Wset": 0,
  "Wupet": "Hold",
  "": null
}
Run Code Online (Sandbox Code Playgroud)

我想将此 Json 文件的输入(每个文件中的属性及其类型可以不同)转换为 C 结构,其中结构自动检测 Json 文件中存在哪些属性(及其类型)。“”中的属性“”:null(可以给定一个随机的属性名)

然后我想自动将Json值的值分配给对象Book1的结构体。

我的代码计划

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

struct Books {
   float  set[50];
   int  low[50];
   char  draw_set[100];
   char  Wupet[100];
};

int main( ) {
    /* Read name.Json …
Run Code Online (Sandbox Code Playgroud)

c json struct

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

写入字符串数组时出现分段错误

我正在尝试将 argv 中传递的一些参数复制到字符串数组中。这是我的程序。

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

int main (int argc, char * argv[]) {
    char **args = malloc(argc * sizeof(char *));
    for (int i = 0; i < argc - 1; ++i) {
        strcpy(*(args+i), argv[i+1]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 for 循环中遇到分段错误。为什么会这样?

c segmentation-fault

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

在C中制作金字塔

我必须使用C,而不是C++制作金字塔.我要做的就是这个,我有一个字符串"这是一个字符串"(是的,假设空间在那里),我必须从两边"删除"一个字母,并打印出来.像这样

"this is a string "
 "his is a string"
  "is a strin"
   "s a stri"
    " a str"
Run Code Online (Sandbox Code Playgroud)

重复此过程,直到没有更多字符.我的老师说要使用子串,C中是否有任何说法

从位置打印(索引0到索引x)
从位置打印(索引1到索引x-1)

我知道我必须使用for循环.

c loops substring

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

标签 统计

c ×6

database ×1

json ×1

loops ×1

malloc ×1

memory ×1

pointers ×1

segmentation-fault ×1

struct ×1

substring ×1