小编Ali*_*pie的帖子

strcmp()如何工作?

我一直在寻找答案.我要做出一系列的我自己的字符串函数一样my_strcmp(),my_strcat()等等.

是否strcmp()通过两个字符数组的每个索引工作,如果ASCII值在两个字符串的相同索引处较小,则该字符串按字母顺序更大,因此返回0或1或2?我想我要问的是,它是否使用字符的ASCII值来返回这些结果?

任何帮助将不胜感激.

[修订]

好的,所以我想出了这个...它适用于所有情况,除非第二个字符串大于第一个字符串.

有小费吗?

int my_strcmp(char s1[], char s2[])
{   
    int i = 0;
    while ( s1[i] != '\0' )
    {
        if( s2[i] == '\0' ) { return 1; }
        else if( s1[i] < s2[i] ) { return -1; }
        else if( s1[i] > s2[i] ) { return 1; }
        i++;
    }   
    return 0;
}


int main (int argc, char *argv[])
{
    int result = my_strcmp(argv[1], argv[2]);

    printf("Value: %d \n", result);

    return …
Run Code Online (Sandbox Code Playgroud)

c string

19
推荐指数
3
解决办法
10万
查看次数

qsort for structures c

以下是我的代码:

我似乎无法有效地使用qsort ...它在填充了名称和开始时间之后将我的数组转换为0 ...这是我的qsort调用的问题吗?或者qsort本身.

带结构的标题如下:

 /**
 * Simulation of a process scheduler
*/

//#ifndef SCHEDULER_H_
#define SCHEDULER_H_

#include <stddef.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>


/* types */
/** units of time */
typedef long time;
/** process identifier */
typedef int pid;

/** Information about a job of interest to the task scheduler */
struct job_data {

/* pid of this process */
    pid pid;
    /* time process starts */
    time …
Run Code Online (Sandbox Code Playgroud)

c text qsort

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

标签 统计

c ×2

qsort ×1

string ×1

text ×1