原理: 在C语言中数组下标访问可以看成指针的偏移访问

1、对表进行检查,看看它是否真的已满

2、如果表确实已满,使用realloc()函数扩展表的长度,并进行检查,确保realloc()操作成功进行。

3、在表中增加所需要的项目

code:

 int current_element=0;

int total_element=128;
char *dynamic=malloc(total_element);
char *ptr;

void add_element(char c){
    
if(current_element==total_element-1){
        total_element
*=2;
        ptr
=(char*)realloc(dynamic,total_element);
        
if(ptr==NULL){
            printf(
"can't expand the table!\n")
            
return -1;
                
            }
            
else
             dynamic
=ptr;
        
        }
        current_element
++;
        dynamic[current_element]
=c;

    }

 

 

相关文章:

  • 2021-09-26
  • 2022-12-23
  • 2022-03-01
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2021-03-27
猜你喜欢
  • 2021-06-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
相关资源
相似解决方案