小编Bil*_*nch的帖子

如何使用指针从函数传递堆上的对象地址

我正在尝试在堆上创建一个对象,然后从调用函数传回它的地址,但我无法让它工作!

如果从main调用此函数,为什么我不能将对象的地址存储在新指针中?

Feline newFeline(int height, int weight) {
  Feline *myFeline = new Feline(height, weight);
  return *myFeline;
}

int main()
{
  Feline *f2;
  *f2 = newFeline(10, 100);
  //cout << f2->getHeight() << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到一个总线错误:10.哦,我喜欢猫.

c++ pointers

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

将事物总结为数组的程序

这本书要求我回答"你对以下程序有什么期望?"

在阅读了很多次后,我似乎并不完全理解它的内部运作.

从我得到的:

  1. First For循环确定此过程将重复10次.变量j被指定为从0开始.
  2. 第二个for循环启动变量i为0并且建立条件i <j并且在其之后写入操作.

到底发生了什么?j从0开始,我也是,因此数字[j] + =数字[i]等于2?

完成此操作后会发生什么?

如果i和j等于0那么为什么这个条件i <j为真?

int main (void)
{
    int numbers[10] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    int i, j;

    for ( j = 0; j < 10; ++j )
        for ( i = 0; i < j; ++i )
            numbers[j] += numbers[i];

    for ( j = 0; j < 10; ++j )
        printf ("%i ", numbers[j]);

    printf ("\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c arrays

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

指向成员属性的指针

我有一个功能f(unsigned char ** data);.另外,我有一个指向pObj 带有unsigned char* pbCert成员的对象的指针.因此,我想执行如下操作:

f(pObj->&pbCert);
Run Code Online (Sandbox Code Playgroud)

不幸的是,我收到了member identifier expected错误.

我设法使用这种方法解决问题:

unsigned char* temp = pObj->pbCert;
f(&temp);
Run Code Online (Sandbox Code Playgroud)

但是我很好奇,是否有一些聪明的记法技巧只在一行中做到这一点?

c

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

在 C++ 中,三角形区域是否显示为零???为什么?

三角形的面积在输出中显示为零,这是为什么?
我做错了什么??

#include <iostream>
using namespace std;

int main() {
  int Base, Height, Area;

  // >>> Is anything wrong with the formula??
  Area = (0.5) * Height * Base;

  cout << "To find the Area of Triangle" << endl << endl;

  // Base
  cout << "Enter Base length:";
  cin >> Base;
  cout << endl;

  // Height
  cout << "Enter Height length";
  cin >> Height;
  cout << endl << endl;

  cout << "Your Base Length is:" << Base << endl;
  cout …
Run Code Online (Sandbox Code Playgroud)

c++ area dev-c++

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

scanf() is not waiting for user input

I am working on making tree using doubly linked list in c. I use recursive call in that function , but somehow it do not works. my code is :

struct node
{
    int data;
    struct node *right;
    struct node *left;
};

struct node* getNode()
{
    struct node *temp;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->right=NULL;
    temp->left=NULL;
    return temp; 
}
Run Code Online (Sandbox Code Playgroud)

here in the below function I am getting the problem.

struct node* maketree()
{
    struct node *t;
    t=getNode(); 
    int value;
    char …
Run Code Online (Sandbox Code Playgroud)

c scanf

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

Python程序使用列表

我正在尝试编写一个显示员工身份,工作时间,工资和工资的程序.

该计划的第二部分要求用户输入员工的ID,以便显示他们的工资.

我的程序的第一部分工作正常,直到pay=input().当我尝试使用第二部分运行它时,它表示存在语法错误.

这是我的计划:

employeeId=[56588,45201,78951,87775,84512,13028,75804]
hours=[40,41,42,43,44,45,46]
payrate=[13.60,13.50,13.40,13.30,13.20,13.10,13.00]
wages=[544.00,553.50,562.80,571.90,580.80,589.50,598.00]
print('employeeId\thours\t\tpayRate\t\twages')
print(employeeId[0],'\t\t',hours[0],'\t\t',payrate[0],'\t\t',wages[0])
print(employeeId[1],'\t\t', hours[1],'\t\t',payrate[1],'\t\t',wages[1])
print(employeeId[2],'\t\t',hours[2],'\t\t',payrate[2],'\t\t',wages[2])
print(employeeId[3],'\t\t',hours[3],'\t\t',payrate[3],'\t\t',wages[3])
print(employeeId[4],'\t\t',hours[4],'\t\t',payrate[4],'\t\t',wages[4])
print(employeeId[5],'\t\t',hours[5],'\t\t',payrate[5],'\t\t',wages[5])
print(employeeId[6],'\t\t',hours[6],'\t\t',payrate[6],'\t\t',wages[6])


pay=input("Would you like to a see a specific employee's gross pay? Y/N:")
if pay=='Y'or pay=='y':
    ID=input('enter employee Id:')
    if ID=='56588':
        print(ID': $',wages[0])
    elif ID=='45201':
        print(ID': $',wages[1])
    elif ID=='78951':
        print(ID': $',wages[2])
    elif ID=='87775':
        print(ID': $',wages[3])
    elif ID=='84512':
        print(ID': $',wages[4])
    elif ID=='13028':
        print(ID': $',wages[5])
    elif ID=='75804':
        print(ID': $',wages[6])
else:
    print(' ')
Run Code Online (Sandbox Code Playgroud)

我看到以下语法错误:

  File "code.py", line 19
    print(ID': $',wages[0])
                ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

用于具有特定功能的循环

我正在处理的程序有一个小问题.

我的程序在数组上执行一个函数.该函数只能执行数组前16个元素的命令.

我现在想要创建一个循环,以便该函数可以在16个以上的数组元素上工作.这是我的想法,但它最终陷入无限循环:

int l = 0;
for (int i=0; i<=size; i+16)
{
    for (int j=0; j<=16;j++)
    {
        FUNCTION(INARRAY; OUTARRAY);
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来的问题是该函数只会遍历数组的16个元素而忽略其余的元素.

让它遍历接下来的16个元素并将其作为以下元素保存在outbuffer中的最佳方法是什么?

当我调整解决方案时,它仍然只处理前16个元素,然后不继续下一个16.

c arrays for-loop

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

如何在(C)中打印链表上的所有节点

我的链表只打印最后一个节点.我怎样才能解决我的问题;

我有一个函数将样本数据填充到链表中,然后打印所有节点.但我的代码只打印此图像:  代码输出是

我的功能是:

void FillSamples()
{
    db = (database *)malloc(sizeof(database));
    db->name = "College";

    tables=NULL;

    char na[5];
    int i = 0;
    for (i = 1; i <= 20; i++) {

        temptable = (table *)malloc(sizeof(table));

        itoa(i, na, 10);

        temptable->name = na;
        temptable->next = tables;

        tables = temptable;
    }

    // create links
    db->tables = tables;

    // print sample
    printf("database name = %s \n", db->name);
    temptable=tables;

    while (temptable)
    {
        printf("tables name = %s \n", temptable->name); 
        temptable=temptable->next;
    }
}
Run Code Online (Sandbox Code Playgroud)

c linked-list

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

数组的初始化

我正在练习的代码的一部分如下:

void outputBarChart()
{
    cout<<"\nOverall grade distribution: "<<endl;
    const int frequencySize = 11;
    int frequency[frequencySize] = {};

    for(int student = 0; student < student; ++student)
        for(int test = 0; test < tests; ++test)
            ++frequency[ (grades[student][test])/10 ];

    for(int counter = 0; counter < frequencySize; ++counter)
    {
        if (counter == 0)
            cout<<" 0-9: "<<endl;
        else if (counter == 10)
            cout<<" 100: "<<endl;
        else
        {
            cout<<counter * 10<<"-"<<(counter*10)+9<<": ";
            for (int stars = 0; stars < frequency[counter]; ++stars)
                cout<<"*";
            cout<<endl;
        }

        //cout<<endl;
    } …
Run Code Online (Sandbox Code Playgroud)

c++ arrays

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

不存在从“std::string”到“const char *”的合适的转换函数 c++

这是一个用于版本号目录的简单程序。它无法编译,出现此错误:

不存在从“std::string”到“const char *”的合适转换函数

#include <iostream>
#include <stdio.h>
#include <io.h>

using namespace std;

int main() {
    int max = 10, daily_patch = 0, monthly_patch = 0, yearly_patch;

    cout << " Enter the yearly_patch Number : ";
    cin >> yearly_patch;

    for (int i = yearly_patch; i <= yearly_patch; i++) {
        for (int j = 0; j < max; j++) {
            for (int k = 0; k < max; k++) {
                string str1 = to_string(i);
                string str2 = to_string(j);
                string str3 = …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

c ×5

c++ ×4

arrays ×3

area ×1

dev-c++ ×1

for-loop ×1

linked-list ×1

pointers ×1

python ×1

python-3.x ×1

scanf ×1