【发布时间】:2009-07-14 14:39:38
【问题描述】:
自从我用 C 写最后一行以来已经有好几年了,现在我试图重新开始做一些功能和使用,而不是作为 php 的扩展。
这是一个创建“小网址”的简单功能
让我们说:
a0bg a0bf a0bh
我遇到的问题是当我必须“增加”像 zzz 这样的字符串时,我得到了:Bus Error
否则,如果我增加 abr,例如我会得到结果:abs
有时我认为我的问题是返回字符串结果
正如我在此处发布的那样,这两个代码都功能齐全。
编译我正在使用:gcc append_id_test.c -o append_id
我在 OS X leopard 上
当我这样做时,它可以工作:(请注意对函数 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;
}
【问题讨论】:
-
我看到 malloc 的但我没有看到 free 的...
-
说“我的一小部分代码可以工作,但是这个具有 4 倍大的其他 2 个函数的其他段不起作用”并不是很有帮助。
-
我只是想向您展示问题的背景:我的想法(我不确定,问题从第二部分的第 74 行开始)
-
您正在尝试修改常量字符串,请查看您的小写转换