gpt4 book ai didi

c - 如何在 c/gcc 中返回脚本?

转载 作者:太空宇宙 更新时间:2023-11-04 07:43:05 25 4
gpt4 key购买 nike

自从我用 C 编写最后一行以来已经有好几年了,现在我正试图重新开始制作一些功能并将其用作 php 的扩展。

这是一个创建“小网址”的简单函数

让我们说:

a0bga0bfa0bh

我遇到的问题是,当我必须“递增”像 zzz 这样的字符串时,我得到了:总线错误

否则,如果我递增 abr,我得到的结果是:abs

有时我认为我的问题是返回字符串结果

这两个代码都可以正常使用,因为我将它们发布在这里。

编译我正在使用:gcc append_id_test.c -o append_id

我在 OS X 豹

当我这样做时,它起作用了:(请注意对函数 append_id 的调用)

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

char* append_id(char*);
int main(void) {
char *x;
char *incremented;

x = "aaab";

printf("%s\n", append_id(x)); // should print 00000 (lenght 5)


incremented = (char *) malloc((strlen(x) + 2) * sizeof(char));
incremented = append_id(x);

printf("---> %s\n", incremented); // should print 00000 (lenght 5)

}

char* append_id(char *id) {

int x;
char* new_id;
int id_size = strlen(id);

new_id = (char *) malloc((strlen(id) + 2) * sizeof(char));

for ( x = 0; x < id_size; x++ )
{
new_id[x] = '0';
}

strcat(new_id, "0");

return new_id;
}

但整个代码无法正常工作(如您所见,对 append_id 函数的调用与上述示例的调用方式相同)

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


char* append_id(char*);
char* increment_id(char*, int);
char* get_next_id(char*);


int main(int argc, char *argv[]) {
char *x;
int a;

x = "zz";

printf("incrementando %s -> %s\n", "zz", get_next_id(x));

return 0;
}


char * get_next_id(char *last_id)
{
int x, pos;
char *next_id;
char is_alnum = 1;

// if the last id is -1 (non-existant), start at the begining with 0
if ( strlen(last_id) == 0 )
{
next_id = "0";
}
else
{

// check the input
for(x = 0; last_id[x]; x++)
{
if(!isalnum(last_id[x]))
{
is_alnum = 0;
break;
}
}

if (is_alnum == 0)
{
return "";
}


// all chars to lowercase
for(x = 0; last_id[x]; x++)
{
last_id[x] = tolower(last_id[x]);
}


// loop through the id string until we find a character to increment
for ( x = 1; x <= strlen(last_id); x++ )
{
pos = strlen(last_id) - x;

if ( last_id[pos] != 'z' )
{
next_id = increment_id(last_id, pos);
break; // <- kill the for loop once we've found our char
}
}

// if every character was already at its max value (z),
// append another character to the string
if ( strlen(next_id) == 0)
{
next_id = (char *) malloc((strlen(last_id) + 2) * sizeof(char));
next_id = append_id(last_id);
}

}


return next_id;
}



char* append_id(char *id) {

int x;
char* new_id;
int id_size = strlen(id);

new_id = (char *) malloc((strlen(id) + 2) * sizeof(char));

for ( x = 0; x < id_size; x++ )
{
new_id[x] = '0';
}

strcat(new_id, "0");

return new_id;
}



char* increment_id(char *id, int pos){
char current, new_char;
char * new_id ;
int x;

new_id = (char *) malloc((strlen(id) + 1) * sizeof(char));


current = id[pos];

if ( current >= 0x30 && current <= 0x39 )
{
if ( current < 0x39 )
{
new_char = current + 1;
}
else // if we're at 9, it's time to move to the alphabet
{
new_char = 'a';
}
}
else // move it up the alphabet
{
new_char = current + 1;
}


for ( x = 0; x < strlen(id); x++ )
{
if (x == pos) {
new_id[x] = new_char;
}
else {
new_id[x] = id[x];
}
}


// set all characters after the one we're modifying to 0
if ( pos != (strlen(new_id) - 1) )
{
for ( x = (pos + 1); x < strlen(new_id); x++ )
{
new_id[x] = '0';
}
}

return new_id;
}

最佳答案

下次请使用 -Wall 编译 ;)

首先,在使用 tolower 和 isalnum 之前,

#include <ctype.h>

其次,您将 x(在 main 作用域中)指定为指向一个字符串文字,它是 const。因此,当您尝试在小写时覆盖它时,您会遇到内存冲突。尝试初始化,比如说,

char x[] = "zz";

您还应该在 get_next_id 中初始化 next_id:

char *next_id = NULL;

稍后,不要在空指针上运行 strlen:

// if every character was already at its max value (z),
// append another character to the string
if (!next_id || !strlen(next_id))
{
next_id = append_id(last_id);
}

在append_id中,使用strcat前需要先null终止:

for ( x = 0; x < id_size; x++ )
{
new_id[x] = '0';
}
new_id[id_size] = 0;

这样就可以正常工作了。您的代码有很多错误。你提到

had been years since I wrote my last line in C

我真的希望你多年前就对它有了更好的理解,看来你对很多基本的 C 内存、字符串和指针概念一无所知。

关于c - 如何在 c/gcc 中返回脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1125865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com