本文介绍了每次运行此代码时,我都会得到3.但它应该是10.如何修复它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

int main()
{
    int n;
    char* element[] = "Hydrogen" ,"Helium", "Lithium", "Beryllium", "Boron", "Carbon", "Nitrogen", "Oxygen", "Fluorine", "Neon" };
    n=strlen(element);
    printf("%d",n);
    return 0;
}





我的尝试:





What I have tried:

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

int main()
{
    int n;
    char* element[] = {"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon"};
    n=strlen(element);
    printf("%d",n);
    return 0;
}

推荐答案

n=strlen(element[0]);

n=strlen(element[1]);

...


#define NELEM(a)  (sizeof(a) / sizeof(a[0]))



尝试,例如


Try, for instance

#include <stdio.h>

#define NELEM(a)  (sizeof(a) / sizeof(a[0]))

int main()
{
    int n;
    char* element[] = { "Hydrogen" ,"Helium", "Lithium", "Beryllium", "Boron", "Carbon", "Nitrogen", "Oxygen", "Fluorine", "Neon" };
    n = NELEM(element);
    printf("%d\n",n);
    return 0;
}



这篇关于每次运行此代码时,我都会得到3.但它应该是10.如何修复它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:01