char [1000]和int [1000]之间有什么区别 [英] What is the difference between char[1000] and int[1000]

查看:200
本文介绍了char [1000]和int [1000]之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

智能女孩|动态编程简介1&算法实践问题| HackerEarth [ ^ ]





来自hackerearth的问题。我已成功提交。但我有些疑惑。



我尝试过的事情:



Intelligent Girl | Introduction to Dynamic Programming 1 & Algorithms Practice Problems | HackerEarth[^]


its a ques from hackerearth. i have submitted successfully. but i have some doubts.

What I have tried:

#include <stdio.h>
int main()
{

    int i,count,j,l;
    char arr[10000];
    scanf("%s",arr);
    
    for(i=0;i<strlen(arr);i++)
    {
            count=0;
    for(j=i;j<strlen(arr);j++)
    {

        if(arr[j]%2==0)
        {

            count++;
        }
    }
        printf("%d ",count);


    }
    return 0;
}

if i will write int arr[10000] instead of char[10000]. it is not working plzz explain me this.

推荐答案

char int 是不同的大小,系统很清楚这一点。 char 变量占用一个字节,而 int 值占用了4天 - 假设一个32位整数,这是正常的这些天。

当你将arr声明为char数组并使用 scanf 来读取字符串时,它读得很好,而你的数组索引从每个字节中获取一个char - 因为索引每次增加一个,所以从中获取每个字节的内存地址。

char and int are different sizes, and the system is well aware of this. char variables occupy a single byte, while int values occupy 4 these days - assuming a 32 bit integer, which is normal these days.
When you declare arr as an array of char and use scanf to read a string into it, it reads it fine, and your array indexes fetch a char from each byte - as the index increases by one each time, so does the memory address from which each is fetched.
----- arr points here
|
v
abcdefghijklm
^^^
|||
||--- arr[2]
||
|---- arr[1]
|
----- arr[0]

当你将它声明为 int 数组时, scanf 做了完全相同的事情,但索引增加了整数大小的内存地址 - 4个字节,而不是一个:

When you declare it as an int array, the scanf does exactly the same thing, but the indexes increase the memory address by the size of an integer - 4 bytes, rather than one:

----------- arr points here
|
v
abcdefghijklm
^   ^   ^
|   |   |
|   |   --- arr[2]
|   |
|   ------- arr[1]
|
----------- arr[0]


数据类型不匹配,但大多数情况下并不能阻止你运行程序,只会给你错误的输出。



但你需要了解int类型和char类型的基本区别,特别是在C语境中。在高级语言中,它们有时具有相同的数据大小。在C中,它们有不同的(很多不同的)大小,char只有1个字节,int是4个字节(在32位上,在其他上不同)。 C数据类型 - 维基百科 [ ^ ]



稍后,您正在尝试执行%d ,需要小数。同样是%s 的情况,它需要一个char类型。因此,您正在以一种格式打印,并在另一种格式中接受,C将永远不会抱怨 - 只有您愿意。您需要更改%d 并使用所有字符,或更改%s 以仅使用整数。此外,完成此更改后,您可以选择要使用的阵列类型。



请在此处阅读, scanf格式字符串 - 维基百科 [ ^ ]
There is a data type mismatch, although mostly it does not prevent you from running the program, only it will give you wrong outputs.

But you need to understand the basic difference in int type and char type, especially in C context. In high-level languages, they sometimes have a same size for data. In C, they have different (a lot different) sizes, char is only 1 byte, and int is 4 bytes (on 32 bit, and different on others). C data types - Wikipedia[^]

Later on, you are trying to do a %d, which expects a decimal. And same is the case for the %s, which expects a char type. So you are printing in one format, and accepting in another, which C will never complain about — only you will. You need to change either %d and work in all characters, or change that %s to work with integers only. Also, once you have made this change, you can select what type of array you want to work with.

Consider reading here, scanf format string - Wikipedia[^]


差异:30000字节的内存。





C 编程语言,A char 是1字节整数,而 int 更大(通常是4字节)。

字符串 char 的数组(即函数处理字符串,如 scanf %s 格式说明符, strlen 指望数组字符作为参数)。

如果在程序中使用 int 数组,则会出现编译器警告(以及不正确的行为)。
Difference: 30000 bytes of memory.


In C programming language, A char is a 1-byte integer while a int is larger (typically 4 bytes).
Strings are array of chars (i.e. functions handling strings, like scanf with %s format specifier, and strlen do expect array of characters as arguments).
You are going to get compiler warnings (and incorrect behaviour) if you use an array of int in your program.


这篇关于char [1000]和int [1000]之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆