使用C语言实现vector动态数组的实例分享

下面是做项目时实现的一个动态数组,先后加入了好几个之后的项目,下面晒下代码。

头文件:

# ifndef __CVECTOR_H__
# define __CVECTOR_H__
# define MIN_LEN 256
# define CVEFAILED -1
# define CVESUCCESS 0
# define CVEPUSHBACK 1
# define CVEPOPBACK 2
# define CVEINSERT  3
# define CVERM    4
# define EXPANED_VAL 1
# define REDUSED_VAL 2 

typedef void *citerator;
typedef struct _cvector *cvector; 

# ifdef _cplusplus
# define EXTERN_ extern "C"
# else
# define EXTERN_ extern
# endif 

EXTERN_ cvector  cvector_create  (const size_t size              );
EXTERN_ void   cvector_destroy (const cvector cv              );
EXTERN_ size_t  cvector_length  (const cvector cv              );
EXTERN_ int    cvector_pushback (const cvector cv, void *memb        );
EXTERN_ int    cvector_popback (const cvector cv, void *memb        );
EXTERN_ size_t  cvector_iter_at (const cvector cv, citerator iter      );
EXTERN_ int    cvector_iter_val (const cvector cv, citerator iter, void *memb);
EXTERN_ citerator cvector_begin  (const cvector cv              );
EXTERN_ citerator cvector_end   (const cvector cv              );
EXTERN_ citerator cvector_next   (const cvector cv, citerator iter      );
EXTERN_ int    cvector_val_at  (const cvector cv, size_t index, void *memb );
EXTERN_ int    cvector_insert  (const cvector cv, citerator iter, void *memb);
EXTERN_ int    cvector_insert_at(const cvector cv, size_t index, void *memb );
EXTERN_ int    cvector_rm    (const cvector cv, citerator iter      );
EXTERN_ int    cvector_rm_at  (const cvector cv, size_t index       );  

/* for test */
EXTERN_ void   cv_info     (const cvector cv              );
EXTERN_ void   cv_print     (const cvector cv              );
#endif /* EOF file cvector.h */

C文件:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <unistd.h>
# define MIN_LEN 256
# define CVEFAILED -1
# define CVESUCCESS 0
# define CVEPUSHBACK 1
# define CVEPOPBACK 2
# define CVEINSERT  3
# define CVERM    4
# define EXPANED_VAL 1
# define REDUSED_VAL 2 

typedef void *citerator;
typedef struct _cvector
{
  void *cv_pdata;
  size_t cv_len, cv_tot_len, cv_size;
} *cvector; 

# define CWARNING_ITER(cv, iter, file, func, line) \
  do {\
    if ((cvector_begin(cv) > iter) || (cvector_end(cv) <= iter)) {\
      fprintf(stderr, "var(" #iter ") warng out of range, "\
          "at file:%s func:%s line:%d!!/n", file, func, line);\
      return CVEFAILED;\
    }\
  } while (0) 

# ifdef _cplusplus
# define EXTERN_ extern "C"
# else
# define EXTERN_ extern
# endif 

EXTERN_ cvector  cvector_create  (const size_t size              );
EXTERN_ void   cvector_destroy (const cvector cv              );
EXTERN_ size_t  cvector_length  (const cvector cv              );
EXTERN_ int    cvector_pushback (const cvector cv, void *memb        );
EXTERN_ int    cvector_popback (const cvector cv, void *memb        );
EXTERN_ size_t  cvector_iter_at (const cvector cv, citerator iter      );
EXTERN_ int    cvector_iter_val (const cvector cv, citerator iter, void *memb);
EXTERN_ citerator cvector_begin  (const cvector cv              );
EXTERN_ citerator cvector_end   (const cvector cv              );
EXTERN_ citerator cvector_next   (const cvector cv, citerator iter      );
EXTERN_ int    cvector_val_at  (const cvector cv, size_t index, void *memb );
EXTERN_ int    cvector_insert  (const cvector cv, citerator iter, void *memb);
EXTERN_ int    cvector_insert_at(const cvector cv, size_t index, void *memb );
EXTERN_ int    cvector_rm    (const cvector cv, citerator iter      );
EXTERN_ int    cvector_rm_at  (const cvector cv, size_t index       ); 

/* for test */
EXTERN_ void   cv_info     (const cvector cv              );
EXTERN_ void   cv_print     (const cvector cv              ); 

cvector cvector_create(const size_t size)
{
  cvector cv = (cvector)malloc(sizeof (struct _cvector)); 

  if (!cv) return NULL; 

  cv->cv_pdata = malloc(MIN_LEN * size); 

  if (!cv->cv_pdata)
  {
    free(cv);
    return NULL;
  } 

  cv->cv_size = size;
  cv->cv_tot_len = MIN_LEN;
  cv->cv_len = 0; 

  return cv;
} 

void cvector_destroy(const cvector cv)
{
  free(cv->cv_pdata);
  free(cv);
  return;
} 

size_t cvector_length(const cvector cv)
{
  return cv->cv_len;
} 

int cvector_pushback(const cvector cv, void *memb)
{
  if (cv->cv_len >= cv->cv_tot_len)
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); 

    if (!cv->cv_pdata)
    {
      cv->cv_pdata = pd_sav;
      cv->cv_tot_len >>= EXPANED_VAL;
      return CVEPUSHBACK;
    }
  } 

  memcpy(cv->cv_pdata + cv->cv_len * cv->cv_size, memb, cv->cv_size);
  cv->cv_len++; 

  return CVESUCCESS;
} 

int cvector_popback(const cvector cv, void *memb)
{
  if (cv->cv_len <= 0) return CVEPOPBACK; 

  cv->cv_len--;
  memcpy(memb, cv->cv_pdata + cv->cv_len * cv->cv_size, cv->cv_size); 

  if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL))
      && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL)))
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len >>= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); 

    if (!cv->cv_pdata)
    {
      cv->cv_tot_len <<= EXPANED_VAL;
      cv->cv_pdata = pd_sav;
      return CVEPOPBACK;
    }
  } 

  return CVESUCCESS;
} 

size_t cvector_iter_at(const cvector cv, citerator iter)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  return (iter - cv->cv_pdata) / cv->cv_size;
} 

int cvector_iter_val(const cvector cv, citerator iter, void *memb)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  memcpy(memb, iter, cv->cv_size);
  return 0;
} 

citerator cvector_begin(const cvector cv)
{
  return cv->cv_pdata;
} 

citerator cvector_end(const cvector cv)
{
  return cv->cv_pdata + (cv->cv_size * cv->cv_len);
} 

static inline void cvmemove_foreward(const cvector cv, void *from, void *to)
{
  size_t size = cv->cv_size;
  void *p;
  for (p = to; p >= from; p -= size) memcpy(p + size, p, size);
  return;
} 

static inline void cvmemove_backward(const cvector cv, void *from, void *to)
{
  memcpy(from, from + cv->cv_size, to - from);
  return;
} 

int cvector_insert(const cvector cv, citerator iter, void *memb)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__); 

  if (cv->cv_len >= cv->cv_tot_len)
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); 

    if (!cv->cv_pdata)
    {
      cv->cv_pdata = pd_sav;
      cv->cv_tot_len >>= EXPANED_VAL;
      return CVEINSERT;
    }
  } 

  cvmemove_foreward(cv, iter, cv->cv_pdata + cv->cv_len * cv->cv_size);
  memcpy(iter, memb, cv->cv_size);
  cv->cv_len++; 

  return CVESUCCESS;
} 

int cvector_insert_at(const cvector cv, size_t index, void *memb)
{
  citerator iter; 

  if (index >= cv->cv_tot_len)
  {
    cv->cv_len = index + 1;
    while (cv->cv_len >= cv->cv_tot_len) cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);
    iter = cv->cv_pdata + cv->cv_size * index;
    memcpy(iter, memb, cv->cv_size);
  }
  else
  {
    iter = cv->cv_pdata + cv->cv_size * index;
    cvector_insert(cv, iter, memb);
  } 

  return 0;
} 

citerator cvector_next(const cvector cv, citerator iter)
{
  return iter + cv->cv_size;
} 

int cvector_val(const cvector cv, citerator iter, void *memb)
{
  memcpy(memb, iter, cv->cv_size);
  return 0;
} 

int cvector_val_at(const cvector cv, size_t index, void *memb)
{
  memcpy(memb, cv->cv_pdata + index * cv->cv_size, cv->cv_size);
  return 0;
} 

int cvector_rm(const cvector cv, citerator iter)
{
  citerator from;
  citerator end;
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  from = iter;
  end = cvector_end(cv);
  memcpy(from, from + cv->cv_size, end - from);
  cv->cv_len--; 

  if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL))
      && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL)))
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len >>= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size); 

    if (!cv->cv_pdata)
    {
      cv->cv_tot_len <<= EXPANED_VAL;
      cv->cv_pdata = pd_sav;
      return CVERM;
    }
  } 

  return CVESUCCESS;
} 

int cvector_rm_at(const cvector cv, size_t index)
{
  citerator iter;
  iter = cv->cv_pdata + cv->cv_size * index;
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  return cvector_rm(cv, iter);
} 

void cv_info(const cvector cv)
{
  printf("/n/ntot :%s : %d/n", __func__, cv->cv_tot_len);
  printf("len :%s : %d/n",   __func__, cv->cv_len);
  printf("size:%s : %d/n/n",  __func__, cv->cv_size);
  return;
} 

void cv_print(const cvector cv)
{
  int num;
  citerator iter; 

  if (cvector_length(cv) == 0)
    fprintf(stderr, "file:%s func:%s line:%d error, null length cvector!!/n", __FILE__, __func__, __LINE__); 

  for (iter = cvector_begin(cv);
      iter != cvector_end(cv);
      iter = cvector_next(cv, iter))
  {
    cvector_iter_val(cv, iter, &num);
    printf("var:%d at:%d/n", num, cvector_iter_at(cv, iter));
  } 

  return;
}

改进版
上面那份代码是在Linux下写的,如果是在Windows的Visul C++环境下编译似乎会出些问题,所以特别做了个改进版:
下面是更新后的代码:

cvector.h

# ifndef __CVECTOR_H__
# define __CVECTOR_H__ 

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

# define MIN_LEN 256
# define CVEFAILED -1
# define CVESUCCESS 0
# define CVEPUSHBACK 1
# define CVEPOPBACK 2
# define CVEINSERT  3
# define CVERM    4
# define EXPANED_VAL 1
# define REDUSED_VAL 2 

typedef void *citerator;
typedef struct _cvector *cvector; 

# ifdef __cplusplus
extern "C" {
# endif 

  cvector  cvector_create  (const size_t size              );
  void   cvector_destroy (const cvector cv              );
  size_t  cvector_length  (const cvector cv              );
  int    cvector_pushback (const cvector cv, void *memb        );
  int    cvector_popback (const cvector cv, void *memb        );
  size_t  cvector_iter_at (const cvector cv, citerator iter      );
  int    cvector_iter_val (const cvector cv, citerator iter, void *memb);
  citerator cvector_begin  (const cvector cv              );
  citerator cvector_end   (const cvector cv              );
  citerator cvector_next   (const cvector cv, citerator iter      );
  int    cvector_val_at  (const cvector cv, size_t index, void *memb );
  int    cvector_insert  (const cvector cv, citerator iter, void *memb);
  int    cvector_insert_at(const cvector cv, size_t index, void *memb );
  int    cvector_rm    (const cvector cv, citerator iter      );
  int    cvector_rm_at  (const cvector cv, size_t index       ); 

  /* for test */
  void   cv_info     (const cvector cv              );
  void   cv_print     (const cvector cv              ); 

# ifdef __cplusplus
}
# endif 

#endif /* EOF file cvector.h */

cvector.c

#include "cvector.h" 

#ifndef __gnu_linux__
#define __func__ "unknown"
#define inline __forceinline
#endif 

# define CWARNING_ITER(cv, iter, file, func, line) \
  do {\
  if ((cvector_begin(cv) > iter) || (cvector_end(cv) <= iter)) {\
  fprintf(stderr, "var(" #iter ") warng out of range, "\
  "at file:%s func:%s line:%d!!\n", file, func, line);\
  return CVEFAILED;\
  }\
  } while (0) 

struct _cvector
{
  void *cv_pdata;
  size_t cv_len, cv_tot_len, cv_size;
}; 

cvector cvector_create(const size_t size)
{
  cvector cv = (cvector)malloc(sizeof (struct _cvector)); 

  if (!cv) return NULL; 

  cv->cv_pdata = malloc(MIN_LEN * size); 

  if (!cv->cv_pdata)
  {
    free(cv);
    return NULL;
  } 

  cv->cv_size = size;
  cv->cv_tot_len = MIN_LEN;
  cv->cv_len = 0; 

  return cv;
}  

void cvector_destroy(const cvector cv)
{
  free(cv->cv_pdata);
  free(cv);
  return;
}  

size_t cvector_length(const cvector cv)
{
  return cv->cv_len;
}  

int cvector_pushback(const cvector cv, void *memb)
{
  if (cv->cv_len >= cv->cv_tot_len)
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);  

    if (!cv->cv_pdata)
    {
      cv->cv_pdata = pd_sav;
      cv->cv_tot_len >>= EXPANED_VAL;
      return CVEPUSHBACK;
    }
  }  

  memcpy((char *)cv->cv_pdata + cv->cv_len * cv->cv_size, memb, cv->cv_size);
  cv->cv_len++;  

  return CVESUCCESS;
}  

int cvector_popback(const cvector cv, void *memb)
{
  if (cv->cv_len <= 0) return CVEPOPBACK;  

  cv->cv_len--;
  memcpy(memb, (char *)cv->cv_pdata + cv->cv_len * cv->cv_size, cv->cv_size);  

  if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL))
    && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL)))
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len >>= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);  

    if (!cv->cv_pdata)
    {
      cv->cv_tot_len <<= EXPANED_VAL;
      cv->cv_pdata = pd_sav;
      return CVEPOPBACK;
    }
  }  

  return CVESUCCESS;
}  

size_t cvector_iter_at(const cvector cv, citerator iter)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  return ((char *)iter - (char *)cv->cv_pdata) / cv->cv_size;
}  

int cvector_iter_val(const cvector cv, citerator iter, void *memb)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  memcpy(memb, iter, cv->cv_size);
  return 0;
}  

citerator cvector_begin(const cvector cv)
{
  return cv->cv_pdata;
}  

citerator cvector_end(const cvector cv)
{
  return (char *)cv->cv_pdata + (cv->cv_size * cv->cv_len);
}  

static inline void cvmemove_foreward(const cvector cv, void *from, void *to)
{
  size_t size = cv->cv_size;
  char *p;
  for (p = (char *)to; p >= (char *)from; p -= size) memcpy(p + size, p, size);
  return;
}  

static inline void cvmemove_backward(const cvector cv, void *from, void *to)
{
  memcpy(from, (char *)from + cv->cv_size, (char *)to - (char *)from);
  return;
}  

int cvector_insert(const cvector cv, citerator iter, void *memb)
{
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);  

  if (cv->cv_len >= cv->cv_tot_len)
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);  

    if (!cv->cv_pdata)
    {
      cv->cv_pdata = pd_sav;
      cv->cv_tot_len >>= EXPANED_VAL;
      return CVEINSERT;
    }
  }  

  cvmemove_foreward(cv, iter, (char *)cv->cv_pdata + cv->cv_len * cv->cv_size);
  memcpy(iter, memb, cv->cv_size);
  cv->cv_len++;  

  return CVESUCCESS;
}  

int cvector_insert_at(const cvector cv, size_t index, void *memb)
{
  citerator iter;  

  if (index >= cv->cv_tot_len)
  {
    cv->cv_len = index + 1;
    while (cv->cv_len >= cv->cv_tot_len) cv->cv_tot_len <<= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);
    iter = (char *)cv->cv_pdata + cv->cv_size * index;
    memcpy(iter, memb, cv->cv_size);
  }
  else
  {
    iter = (char *)cv->cv_pdata + cv->cv_size * index;
    cvector_insert(cv, iter, memb);
  }  

  return 0;
}  

citerator cvector_next(const cvector cv, citerator iter)
{
  return (char *)iter + cv->cv_size;
}  

int cvector_val(const cvector cv, citerator iter, void *memb)
{
  memcpy(memb, iter, cv->cv_size);
  return 0;
}  

int cvector_val_at(const cvector cv, size_t index, void *memb)
{
  memcpy(memb, (char *)cv->cv_pdata + index * cv->cv_size, cv->cv_size);
  return 0;
}  

int cvector_rm(const cvector cv, citerator iter)
{
  citerator from;
  citerator end;
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  from = iter;
  end = cvector_end(cv);
  memcpy(from, (char *)from + cv->cv_size, (char *)end - (char *)from);
  cv->cv_len--;  

  if ((cv->cv_tot_len >= (MIN_LEN << REDUSED_VAL))
    && (cv->cv_len <= (cv->cv_tot_len >> REDUSED_VAL)))
  {
    void *pd_sav = cv->cv_pdata;
    cv->cv_tot_len >>= EXPANED_VAL;
    cv->cv_pdata = realloc(cv->cv_pdata, cv->cv_tot_len * cv->cv_size);  

    if (!cv->cv_pdata)
    {
      cv->cv_tot_len <<= EXPANED_VAL;
      cv->cv_pdata = pd_sav;
      return CVERM;
    }
  }  

  return CVESUCCESS;
}  

int cvector_rm_at(const cvector cv, size_t index)
{
  citerator iter;
  iter = (char *)cv->cv_pdata + cv->cv_size * index;
  CWARNING_ITER(cv, iter, __FILE__, __func__, __LINE__);
  return cvector_rm(cv, iter);
}  

void cv_info(const cvector cv)
{
  printf("\n\ntot :%s : %d\n", __func__, cv->cv_tot_len);
  printf("len :%s : %d\n",   __func__, cv->cv_len);
  printf("size:%s : %d\n\n",  __func__, cv->cv_size);
  return;
}  

void cv_print(const cvector cv)
{
  int num;
  citerator iter;  

  if (cvector_length(cv) == 0)
    fprintf(stderr, "file:%s func:%s line:%d error, null length cvector!!\n", __FILE__, __func__, __LINE__);  

  for (iter = cvector_begin(cv);
    iter != cvector_end(cv);
    iter = cvector_next(cv, iter))
  {
    cvector_iter_val(cv, iter, &num);
    printf("var:%d at:%d\n", num, cvector_iter_at(cv, iter));
  }  

  return;
}

main.cpp

#include "cvector.h" 

int main()
{
  int i = 1;
  cvector cv = cvector_create(sizeof(int));
  cvector_pushback(cv, &i);
  cvector_pushback(cv, &i);
  cvector_pushback(cv, &i);
  cvector_pushback(cv, &i);
  cv_print(cv);
  cvector_destroy(cv);
  return 0;
} 
(0)

相关推荐

  • C语言十进制转二进制代码实例

    用C语言实现将十进制转化为二进制,并统计转换后的二进制码中1的个数. #include <stdio.h> int binaryNum[16]; //存放转换后得到的二进制码 int count=0; //计数十进制整数被2除的次数 int oneCount=0; //得到的二进制码中1的个数 void main(){ int num; printf("输入一个十进制的整数:"); scanf("%d",&num); while( (num/2)

  • C语言完美实现动态数组代码分享

    我们知道,C语言中的数组大小是固定的,定义的时候必须要给一个常量值,不能是变量. 这带来了很大的不便,如果数组过小,不能容下所有数组,如果过大,浪费资源. 请实现一个简单的动态数组,能够随时改变大小,不会溢出,也不会浪费内存空间. 下面的代码实现了简单的动态数组: #include <stdio.h> #include <stdlib.h> int main() { //从控制台获取初始数组大小 int N; int *a; int i; printf("Input ar

  • 深入分析C语言中结构体指针的定义与引用详解

    指向结构体类型变量的使用首先让我们定义结构体:struct stu{char name[20];long number;float score[4];} ;再定义指向结构体类型变量的指针变量:struct stu *p1, *p2 ;定义指针变量p 1.p 2,分别指向结构体类型变量.引用形式为:指针变量→成员:[例7-2] 对指向结构体类型变量的正确使用.输入一个结构体类型变量的成员,并输出. 复制代码 代码如下: #include <stdlib.h> /*使用m a l l o c (

  • C语言中system()函数的用法总结

    system()函数功能强大,很多人用却对它的原理知之甚少先看linux版system函数的源码: 复制代码 代码如下: #include <sys/types.h>#include <sys/wait.h>#include <errno.h>#include <unistd.h> int system(const char * cmdstring){    pid_t pid;    int status; if(cmdstring == NULL){  

  • c语言字符数组与字符串的使用详解

    1.字符数组的定义与初始化字符数组的初始化,最容易理解的方式就是逐个字符赋给数组中各元素.char str[10]={ 'I',' ','a','m',' ','h','a','p','p','y'};即把10个字符分别赋给str[0]到str[9]10个元素如果花括号中提供的字符个数大于数组长度,则按语法错误处理:若小于数组长度,则只将这些字符数组中前面那些元素,其余的元素自动定为空字符(即 '\0' ). 2.字符数组与字符串在c语言中,将字符串作为字符数组来处理.(c++中不是)在实际应用

  • c语言动态数组示例

    复制代码 代码如下: #include <stdio.h>#include <stdlib.h> int main(){    //从控制台获取初始数组大小    int N;    printf("Input array length:");    scanf("%d",&N);    printf("\n"); //分配空间    int *a;    a=(int *)calloc(N,sizeof(int)

  • C语言socket编程开发应用示例

    实现步骤: 1. Server端 复制代码 代码如下: #include <stdio.h>                   //用于printf等函数的调用#include <winsock2.h>                //Socket的函数调用 #pragma comment (lib, "ws2_32")      //C语言引用其他类库时,除了.h文件外,还要加入对应的lib文件(这个不同于C#) 复制代码 代码如下: int main()

  • C语言动态数组的使用实现代码

    C语言动态数组 从键盘读入n个整数,使用动态数组存储所读入的整数,并计算它们的和与平均值分别输出.要求尽可能使用函数实现程序代码.平均值为小数的只保留其整数部分. 样例输入: 5 3 4 0 0 2 样例输出: 9  1 样例输入: 7 3 2 7 5 2 9 1 样例输出: 29  4 代码如下: #include<stdio.h> int addAll(int a[],int N); int aveFun(int sum,int n); int main(){ int N; int sum

  • linux根据pid获取进程名和获取进程pid(c语言获取pid)

    Liunx中通过进程名查找进程PID可以通过 pidof [进程名] 来查找.反过来 ,相同通过PID查找进程名则没有相关命令.在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当前运行的所有进程都对应于该目录下的一个以进程PID命名的文件夹,其中存放进程运行的N多信息.其中有一个status文件,cat显示该文件, 第一行的Name即为进程名. 打开stardict程序,进程名为stardict: shell中分别根据Pid获取进程名.根据进程名获取Pid 1)查找stard

  • C语言构建动态数组完整实例

    本文以一个完整的实例代码简述了C语言构建动态数组的方法,供大家参考,完整实例如下: #include <stdio.h> #include <malloc.h> int main(void) { int len; int * arr; printf("请输入数组长度:"); scanf("%d", &len); arr = (int *)malloc(sizeof(int)*len); printf("请输入数组的值:&qu

  • C语言运算符优先级列表(超详细)

    每当想找哪个运算符优先级高时,很多时候总是想找的就没有,真让人气愤!现在,终于有个我个人觉得非常全的,分享给大家,欢迎拍砖! C语言运算符优先级 优先级 运算符 名称或含义 使用形式 结合方向 说明 1 [] 数组下标 数组名[常量表达式] 左到右 -- () 圆括号 (表达式)/函数名(形参表) -- . 成员选择(对象) 对象.成员名 -- -> 成员选择(指针) 对象指针->成员名 -- 2 - 负号运算符 -表达式 右到左 单目运算符 ~ 按位取反运算符 ~表达式 ++ 自增运算符 +

  • c语言冒泡排序法代码

    总在写 总在错, 面试也还忘记 学习就是这么个过程, 温故才知新, 望自己谨记 忘记不要紧 复习就好 //排序是有很多种方法的 ,完成从小到大的排列 复制代码 代码如下: #include <stdio.h>void sort(int *a,int len){int i=0; int j; int t;    for(i=0;i<len;i++)    {        for(j=0;j<len-i-1;j++)        {            if(a[j]>a[

  • 基于C语言实现的贪吃蛇游戏完整实例代码

    本文以实例的形式讲述了基于C语言实现的贪吃蛇游戏代码,这是一个比较常见的游戏,代码备有比较详细的注释,对于读者理解有一定的帮助. 贪吃蛇完整实现代码如下: #include <graphics.h> #include <conio.h> #include <stdlib.h> #include <dos.h> #define NULL 0 #define UP 18432 #define DOWN 20480 #define LEFT 19200 #defi

随机推荐