小编Sig*_*iSv的帖子

我们如何找到以下C程序的输出?

我找到了以下C程序的输出,我在GeeksforGeeks上找到了它.这是程序:

#include <stdio.h>
void fun(int ptr[])
{
    int i;
    unsigned int n = sizeof(ptr)/sizeof(ptr[0]);
    for (i=0; i<n; i++)
    printf("%d  ", ptr[i]);
}

// Driver program
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    fun(arr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码的输出为" 1 2 ".但据我说,输出应该只有1.以下是我对此代码的解释:

  1. 首先,将运行main函数,在声明数组"arr"之后,将执行包含语句fun(arr)的下一个语句.
  2. 在该语句中,函数"fun"将使用参数arr调用,该参数包含数组的第一个元素的地址.
  3. 之后,在函数fun下,有一个指针ptr作为参数.当执行此函数时,n的值将计算为1,因为此处ptr的大小为4,ptr [0]的大小也为4.
  4. 接下来,循环将仅运行一次,因为n的值为1,这就是为什么只有'1'将被打印,因为它是ptr [0]的值.

请帮我弄清楚我错在哪里.

c pointers sizeof

4
推荐指数
2
解决办法
209
查看次数

在另一个函数中调用函数,导致由于parantheses中的参数而导致错误

碰巧我正在使用Python进行编程,我正准备编写一个小石头剪刀游戏.

不幸的是,当我尝试运行我的脚本时,我收到以下错误:

file rps.py, line 53 in game    
   compare (move,choice)     
  NameError: name 'move' is not defined"
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我的代码:

from random import randint
possibilities = ['rock', 'paper', 'scissors']

def CPU(list):
    i =  randint(0, len(list)-1)
    move = list[i]
    #print (str(move))
    return move

def User():
    choice = str(input('Your choice? (Rock [r], Paper[p], Scissors[s])'))
    choice = choice.lower()

    if choice == 'rock' or choice == 'r':
        choice = 'rock'
    elif choice == 'scissors' or choice =='s':
        choice = 'scissors'
    elif choice == 'paper' or choice == …
Run Code Online (Sandbox Code Playgroud)

python function

3
推荐指数
1
解决办法
48
查看次数

是否可以从C语言函数写入word文件?

我有一个用C语言编写的库管理系统,里面有I/O文件.dat.如何从这个函数中获取word文件的输出:

void viewbooks(void)  //show the list of book persists in library
{
    int i=0,j;
    system("cls");
    gotoxy(1,1);
    printf("*********************************Book List*****************************");
    gotoxy(2,2);
    printf(" CATEGORY     ID    BOOK NAME     AUTHOR       QTY     PRICE     RackNo ");
    j=4;
    fp=fopen("Bibek.dat","rb"); //the .dat file getting data to be showed
    while(fread(&a,sizeof(a),1,fp)==1) // .dat file to be read
    {
        gotoxy(3,j);
        printf("%s",a.cat);
        gotoxy(16,j);
        printf("%d",a.id);
        gotoxy(22,j);
        printf("%s",a.name);
        gotoxy(36,j);
        printf("%s",a.Author);
        gotoxy(50,j);
        printf("%d",a.quantity);
        gotoxy(57,j);
        printf("%.2f",a.Price);
        gotoxy(69,j);
        printf("%d",a.rackno);
        printf("\n\n");
        j++;
        i=i+a.quantity;
    }
    gotoxy(3,25);
    printf("Total Books =%d",i);
    fclose(fp);
    gotoxy(35,25);
    returnfunc();
}
Run Code Online (Sandbox Code Playgroud)

c ms-word

2
推荐指数
1
解决办法
113
查看次数

减少C中的语句以提高效率

我试图通过减少条件语句的数量来提高我的代码效率,因为我将来会在汇编中编写代码.你们有没有办法减少陈述的效率?

if (j==18)
{
    store=12;
}
else if(j==30)
{
    store=10;
}
else if(j==42)
{
    store=8;
}
else if(j==54)
{
    store=6;
}
else if(j==66)
{
    store=4;
}
else if(j==78)
{
    store=2;
}
else if(j==92)
{
    store=2;
}
else if(j==106)
{
    store=4;
}
else if(j==120)
{
    store=6;
}
else if(j==134)
{
    store=8;
}
else if(j==148)
{
    store=10;
}
else if(j==162)
{
    store=12;
}
else store=1;
Run Code Online (Sandbox Code Playgroud)

c reduce assembly conditional if-statement

2
推荐指数
1
解决办法
228
查看次数

使用C++将用户定义的数据类型保存到文件中

我有一个这样的课:

struct SomeClass{

    SomeClass(){}

    SomeClass(const SomeClass& rhs):
    _m1(rhs._m1),
    _m2(rhs._m2),
    _m3(rhs._m3),
    _m4(rhs._m4){}

    SomeClass& operator=(const SomeClass& rhs){
    // same as above
    return *this;
    }

    int _m1;
    std::vector<int> _m2;
    std::vector<int> _m3;
    int _m4;
};
Run Code Online (Sandbox Code Playgroud)

在我的程序中的某个时刻,我想保存存储在SomeClass对象中的数据供以后使用:

    SomeClass someObj = arr->getBest(); // arr is a pointer to AnotherClass,
                                        // in which different SomeClass
                                        // objects are initialized and then
                                        // involved in various 
                                        // computations in AnotherClass, 
                                        // finally the best one SomeClass 
                                        // object will be save here
    fwrite(&someObj, sizeof(SomeClass), 1, saveFile); …
Run Code Online (Sandbox Code Playgroud)

c++ fwrite fread

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

为什么输出0.000000?

有人可以解释为什么这两个变量"area"和"peri"输出0.000000?

int main()
{

    double length, width, peri = (length*2) + (width*2), area = length*width;

    printf("Enter the length of the rectangle in centimeters: ");
    scanf("%lf", &length);
    printf("\nEnter the width of the rectangle in centimeters: ");
    scanf("%lf", &width);

    printf("The perimeter of the rectangle is %lf cm.\nThe area of the rectangle is %lf cm.", circ, area);


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c

-1
推荐指数
1
解决办法
106
查看次数

标签 统计

c ×4

assembly ×1

c++ ×1

conditional ×1

fread ×1

function ×1

fwrite ×1

if-statement ×1

ms-word ×1

pointers ×1

python ×1

reduce ×1

sizeof ×1