小编Dzu*_*yen的帖子

传递值时更改整数数组的大小

我只是好奇出现以下错误的原因是什么:我正在尝试确定整数数组的大小.

当我在main函数中找到数组的大小时,此代码返回正确:

 int program[12] = { 1007, 1008, 2007, 3008, 2109, 1109, 4300, 0, 0, 0, -99999 };
 cout<<sizeof(program)/sizeof(program[0])<<"\n";
Run Code Online (Sandbox Code Playgroud)

但是如果我按程序将程序传递给函数,它总是返回2:

void Sipmletron::load(int program[])
{
    int length=sizeof(program)/sizeof(program[0])
    std::cout<<length<<"\n";
    memory= new int[length];
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays

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

Deque实现中的空指针异常

我正在学习Java,并实现Deque数据结构.这是Node类:

import java.util.*;

public class Deque<Item> implements Iterable<Item> {
    private Node sentinel;

    private class Node {
        Item item;
        Node next;
        Node previous;
        Node(Item value) {
            item = value;
            next = this;
            previous = this;
        }
    }

    public Deque(Item item)                           // construct an empty deque
    {
        Node sentinel = new Node(item);
    }

    public boolean isEmpty()                 // is the deque empty?
    {
        return (size() == 0);
    }
    public int size()                        // return the number of items on the deque
    {
        System.out.println("size");
        if …
Run Code Online (Sandbox Code Playgroud)

java deque

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

使用cutoff从C转换为C中的char

我需要在C中将int转换为unsigned char.如果int> 255 char将是255,如果in <0 char将为0.否则char等于int.我怎么用C做到这一点?

我试过了:

int i;
unsigned char c;
c = (unsigned char) i;
Run Code Online (Sandbox Code Playgroud)

但它尚未奏效(它包裹,即c = i%256).

c

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

错误:无法将'unsigned char*'转换为'const int*'

我正在编写一个函数来接受unsigned char*和int*:

int foo(const int * a)
Run Code Online (Sandbox Code Playgroud)

但是当我将unsigned char*a传递给foo()时,它有这个编译错误.我以为unsigned char会自动转换为int吗?(较小的范围变量将被铸造成较大的变量).它也适用于数组指针吗?

示例代码:

#include <iostream>
using namespace std;

int average(const int * array, int size)
{
    int sum = 0;
    for (int i = 0; i <size; i++)
    sum+=*(array+i);
    return sum/size;
}

int main() {
    unsigned char array[5] = {0,1,2,3,4};
    std::cout << average(array, 5);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编写一个接受unsigned char*和int*的函数的最佳方法是什么?

c++

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

标签 统计

c++ ×2

arrays ×1

c ×1

deque ×1

java ×1