SIZEOF返回不正确数组的长度 [英] Sizeof returning incorrect array length

查看:165
本文介绍了SIZEOF返回不正确数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1975128/sizeof-an-array-in-the-c-programming-language\">Sizeof在C编程语言中数组?


我一直用C摆弄成为它更好地了解和想我可能已经在一个初始化/指针的问题,我不能确定如何解决跌跌撞撞。下面方案是ROT13的实现,因此它由13取输入字符串,并且将每个字母,产生密文。我的程序的输出显示正确的转变,但不会超过4个字符的工作,使我不知道是否被正确使用的sizeof。任何其他的建议是AP preciated,我敢肯定,我在这一点上搞砸了一些东西。

 的#include&LT;&stdio.h中GT;
#包括LT&;&string.h中GT;空加密(字符*);诠释主要(无效){    字符输入[] =fascs;
    加密(输入);    返回0;
}空加密(字符*输入){    字符字母[] =ABCDEFGHIJKLMNOPQRSTUVWXYZ;    的printf(输入:%S \\ n,输入);    INT inputCount = sizeof的(输入);    的printf(中输入字符:%我的\\ n \\ n,inputCount);    //保存加密的文本
    焦炭encryptedOutput [inputCount]    //初始化计数器
    INT I,J = 0;    //遍历数组字母,如果输入=当前字母,移位13模(26)
    //推结果输出数组,encryptedOutput
    对于(i = 0; I&LT; inputCount;我++){
        为(J = 0; J&LT; 26; J ++){
            如果(输入[I] ==字母[J]){
                encryptedOutput [I] =字母[(J + 13)%26];
            }
        }
    }    //讷终止印刷目的
    encryptedOutput [I] ='\\ 0';    的printf(腐13%S \\ n \\ n,encryptedOutput);}


解决方案

的sizeof()加密不会表现为你想让它。在加密的sizeof(字符*) 4 (32位机器上)或 8 ,你可以看到的是一个指针的大小(在64位计算机上)。

要获得的sizeof(输入)您必须更改的sizeof 的strlen 。因此,解= 的strlen(输入)

为什么出现这种情况?当你传递一个数组到一个函数,该阵列内部进行了重新psented为指针$ P $。在所谓的功能结束输入是的只是的指针,这使无论是 4 8 bytesize取决于你的机器。

要获得的sizeof 输入,只需用这样的宏:
的#define SIZEOF(X)(sizeof的(X)/ sizeof的(X [0]))
并且在的函数定义 X 使用。在你的程序中, X 输入的main()

Possible Duplicate:
Sizeof an array in the C programming language?

I've been fiddling with C to become better acquainted with it and think I may have stumbled upon a initialization/pointer issue that I'm unsure of how to resolve. The below program is an implementation of ROT13, so it takes an input string, and shifts each letter by 13, resulting in the cipher text. The output of my program displays the correct shift, but it won't work for more than 4 characters, making me wonder if sizeof is being used incorrectly. Any other suggestions are appreciated, I'm sure I've messed a few things up at this point.

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

void encrypt(char *);

int main(void){

    char input[] = "fascs";
    encrypt(input);

    return 0;
}

void encrypt(char *input){

    char alphabet[] = "abcdefghijklmnopqrstuvwxyz";

    printf("Input: %s \n", input);

    int inputCount = sizeof(input);

    printf("Characters in Input: %i \n\n", inputCount);

    //holds encrypted text
    char encryptedOutput[inputCount];

    //Initialize counters
    int i, j = 0;

    // loop through alphabet array, if input=current letter, shift 13 mod(26),
    // push result to output array, encryptedOutput
    for(i = 0; i < inputCount; i++){
        for(j = 0; j < 26; j++){
            if(input[i] == alphabet[j]){
                encryptedOutput[i] = alphabet[(j + 13) % 26];
            }
        }
    }

    //Nul Termination for printing purposes
    encryptedOutput[i] = '\0';

    printf("Rot 13: %s \n\n", encryptedOutput);

}

解决方案

sizeof() in encrypt will not behave as you want it to. Inside encrypt, the sizeof(char *) is 4(on a 32bit machine) or 8(on a 64 bit machine), which you can see is the size of a pointer.

To get the sizeof(input) you must change sizeof to strlen. Hence solution = strlen(input)

Why this happens?? when you pass an array into a function, that array is internally represented as a pointer. At the called-function's end input is just a pointer, which gives either 4 or 8 bytesize depending upon your machine.

To get the sizeof of input, just use a macro like this: #define SIZEOF(x) (sizeof(x)/sizeof(x[0])) and use this in the function that defines x. In your program, x is input in main()

这篇关于SIZEOF返回不正确数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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