标签: pointers

Struct,Pointers中的数组[C++ Beginner]

来自Java,PHP背景,我试图进入C++.我想在一个结构中存储一个数组.我的问题是在初始化结构后指定数组的大小.

这是我的结构代码:

struct SpriteAnimation {
    // ...
    int parts;                  // total number of animation-parts
    unsigned int textures[];    // array to store all animation-parts
    // ...
};
Run Code Online (Sandbox Code Playgroud)

这里主要功能:

SpriteAnimation bg_anim;
bg_anim.parts = 3; 
unsigned int *myarray = new unsigned int[bg_anim.parts];
bg_anim.textures = myarray;
Run Code Online (Sandbox Code Playgroud)

我需要更改什么来解决这个问题?

c++ arrays struct pointers

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

C++传递指针

我期待下面的代码打印1,但它打印一个随机的大数字.我不明白为什么会这样,请指教.

#include <iostream>
using namespace std;

int * returnArray()
{
    int myArray[5]={1,2,3,4,5};
    return myArray;
}

void printArray(int * myArray)
{
    cout << *myArray<< endl;
}

int main()
{
    printArray(returnArray());
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers scope

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

C指针:怎么说这段代码

我发现这个页面有以下指针解释:http:
//www.woyouxian.net/c/c0501.html

int x = 1, y = 2, z[10];
int *ip;          /* ip is a pointer to int */
ip = &x;          /* ip now points to x */
y = *ip;          /* y is now 1 */
*ip = 0;          /* x is now 0 */
ip = &z[0];       /* ip now points to z[0] */
Run Code Online (Sandbox Code Playgroud)

但是,行"y现在是1""x现在是0"描述了结果,而不是代码.如何"说"那些线来描述代码(如其他线那样)?

换句话说,行"y现在为1"并不隐含地在行上具有文字"1",因此描述描述了代码的结果而不是代码本身.我想要一个代码本身的描述.

c pointers

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

返回alloca指针

此代码是否返回对堆栈上分配的变量的无效引用?或者是什么:

void *f(size_t sz) {
    return alloca(sz);
}
Run Code Online (Sandbox Code Playgroud)

或者这是一个由alloca实现/编译器支持处理的特殊情况f(alloca(size), alloca(size))

c stack pointers memory-management alloca

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

C指针算术问题

我有一个小指针算术和一般指针,我把这个代码拉到一起.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

int main(int argc,char **argv){
    void *ptr=NULL;
    char a='a';
    int i0=0;
    char b='b';
    int i1=1;
    char c='c';
    int i2=2;
    ptr=&a;
    //What do I do here to get to i0?
    printf("%d",(int*)ptr); //and to print it out?
    while(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题正是我在评论中提出的问题.如何在不执行'ptr =&i0;'的情况下让ptr指向i0 使用指针算术?另外,我如何正确地为字符和整数打印出来(一个用于char的方法,一个用于int的方法).

提前致谢.;)

c math pointers

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

在C中使用传递的引用struct变量

我有一个基本的指针问题.我有一些这样的代码:如果以下代码中有任何错误,请告诉我:

struct abc {
  int a;
  int b;
};

void func2(int*); // defined elsewhere

void func1 (struct abc *p1)
{
  struct abc var1 = *p1; // ======> Can I do this ? 

  func2(&var1.b);
  func2(&p1->b);      // =========> Which of these 2 is right ? 
}
Run Code Online (Sandbox Code Playgroud)

c pointers

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

C++用数字填充数组然后返回指向它的指针

我最近遇到了一个问题,我想不出一个好的解决方法.我正在使用案例结构来尝试将属性设置为将传递给对象构造函数的"字符".

例:

//note this is inside a function with a return type of int*
int selection;
cin >> selection;
int * iPtr;

switch(selection){

case 1:{
     int anArray[6] = {8,5,2,4,250,100} // str, dex, int, luck, hp, mp
     iPtr = anArray;
     return iPtr;
}

//more cases and such below
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,当我返回指针时,它似乎充满了大量的垃圾,而不是信息,而不是我期望它保留的信息.那是因为数组在范围的末尾被销毁了吗?如果是这样,我该怎么做才能使我的方法成为我希望它(获得一个指向我想要的值的指针).

谢谢!

c++ pointers

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

"不兼容的指针类型"在C中编译

我的代码

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

void getData(short int *number, char *string)
{
    printf("\nPlease enter a number greater than zero: ");
    scanf("%hd", number);

    printf("Please enter a character string: ");
    scanf("%s", string);
}

void echoPair(short int *number, char *string)
{
    printf("Number: %hd Character(s): %s\n", *number, string); 
}

int main()
{
    short int *number = 0;
    char string[32] = {0};

    printf("This program will ask you to enter a number greater than zero and \na character string with less than 32 characters …
Run Code Online (Sandbox Code Playgroud)

compiler-construction pointers character short compiler-warnings

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

如何在链表的节点之间移动?

这是一段试图构建链表的代码.

struct node {
    char name[20];
    int age;
    int height;
    node* next; // Pointer to the next node
};
node* startPTR = NULL;

void addNode_AT_END() {
    node *temp1;
    node *temp2;

    temp1 = new node;  

    cout << "Enter the name : ";
    cin  >> temp1->name;
    cout << endl << "Enter the age : ";
    cin  >> temp1->age;
    cout << endl << "Enter height : ";
    cin  >> temp1->height;

    temp1->next = NULL;

    if( startPTR == NULL) {
       startPTR = temp1; 
    }  else …
Run Code Online (Sandbox Code Playgroud)

c++ pointers linked-list list

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

为什么fclose没有将文件指针设置为NULL?

我正在为FILE*编写RAII包装器.我注意到,当在析构函数中关闭后删除FILE*会导致未定义的行为(seg.错误或其他地方的错误).我假设fclose会将FILE*设置为NULL,但事实并非如此.

class smartFP {
  smartFP (const std::string& name) 
  : fp (fopen(name.c_str(), "r")
  { }

  ~smartFP()
  { 
     if (fp) {
        fclose(fp); 
        // delete(fp); <- This is causing crash
        fp = NULL; <- Is this OK?
     }
  }

private:
   FILE *fp;
};
Run Code Online (Sandbox Code Playgroud)
  • 为什么fclose没有将FILE*设置为NULL?
  • 第二个问题是fopen在堆或堆栈中为fp分配内存吗?我认为它在堆上做了,因此想在fclose之后进行删除,这样fp的堆上的4或8个字节就被解除分配了.但看起来这不是必需的.

c++ pointers fclose

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