标签: pointers

无法理解如何从C中的函数返回字符串

    void add()
    {    
    char name[50], surname[50], usern[50];

    int id, birth, amount;
    printf("Enter your name, surname and your year of birth:\n");
    scanf("%s %s %d", &name, &surname, &birth);
    printf("Enter your ID, username and your total amount:\n");
    scanf("%d %s %d", &id, &usern, &amount);
    const char *pass1=function(usern);  
    }

  const char *function (char usern[50])
  {
    char temp;
    int i=0;
    int j;
    j = strlen(usern) - 1;
    while (i < j) 
    {
      temp = usern[i];
      usern[i] = usern[j];
      usern[j] = temp;
      i++;
      j--;
    }
    return usern; …
Run Code Online (Sandbox Code Playgroud)

c string pointers function

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

使用STL队列pop()销毁类指针

当您获得一个指针some_queue.front(),将其分配给另一个变量,然后调用时会发生some_queue.pop()什么?谁应该清理内存?(我使用的是c ++ 98,如果确实需要,我可以使用boost智能指针)

例如:(为什么要这样做?还是不应该这样做?)

class SClass {
public:
    SClass(int si): sInt(si){}
    int getSInt(){ return sInt; }

private:
    int sInt;
    ... // bunch of other complicated data types so copy might be slow
};

int main()
{
   cout << "Hello World" << endl; 
   queue<SClass *> sq;
   for(int i = 0; i < 10; i++){
       SClass *sc = new SClass(i);
       sq.push(sc);
   }
   SClass *s2 = NULL;
   while(!sq.empty()){
       s2 = sq.front();
       sq.pop();
       cout << s2->getSInt() << endl;
   }
   delete …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

C++指针分配不起作用(关闭34个字节)

在大型单线程C++应用程序(使用GCC 4.4.7 20120313编译)中,简单指针赋值不等于原始指针值:

class DvComVectorStreamBase : virtual public std::ios
{
    // some stuff here
};

class DvComVectorOStream : public DvComVectorStreamBase, public std::ostream
{
public:
    DvComVectorOStream(int which = std::ios::out, size_t capacity = 0) :
        DvComVectorStreamBase(which, capacity)
    {}
};


class Formatter : public aStandAloneClass
{
    // cruft removed

protected:
    void initFunction();

    ostream*                 mpStream;
    DvComVectorOStream*      mpVectorOStream;
}

Formatter:initFunction()
{
    printf("mpStream %p mpVectorOStream %p\n", (void *)mpStream, (void *)mpVectorOStream);
    if (mpVectorOStream == 0)
        mpVectorOStream = new DvComVectorOStream(ios::out, 32768);
    else
        mpVectorOStream->clear();

    printf("mpStream %p mpVectorOStream %p\n", (void …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

指针不会在类成员函数中被修改

我正在制作一个包含模板和类的动态数组.

这是我遇到问题的代码:

template<typename GType>
class GArray
{
   GType* array_type = nullptr;
   int size = 0;

public:
GArray(GType Size)
{
    size = Size;
    array_type = new GType[size];

    for (int i = 0; i < size; i++)
        array_type[i] = NULL;
}

void Push(GType Item)
{
    size++;

    GType* temp = new GType[size];

    for (int i = 0; i < size-1; i++)
        temp[i] = array_type[i];

    temp[size] = Item;

    delete[] array_type;
    array_type = temp;
    temp = nullptr;
}

GType& operator[] (int Index)
{
    if (Index …
Run Code Online (Sandbox Code Playgroud)

c++ pointers

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

解除指向不完整类型结构的指针

当我尝试编译时,我得到一个错误说:"解除指向不完整类型struct Freunde的指针"

那是我的结构:

typedef struct {
    char *Name;
    struct Freunde *next;
} Freunde;
Run Code Online (Sandbox Code Playgroud)

错误发生在这里:

while (strcmp(Anfang->next->Name, Name) != 0)
    Anfang = Anfang->next;
Run Code Online (Sandbox Code Playgroud)

编辑///所以这里是我尝试运行的程序中的更多代码:

void add(Freunde* Anfang, char* Name) {
    Freunde * naechster;

    while (Anfang->next != NULL) {
        Anfang = Anfang->next;
    }
    Anfang->next = (Freunde*) malloc(sizeof(Freunde));
    naechster = Anfang->next;
    naechster->Name = Name;
    naechster->next = NULL;

}


int main() {
    Freunde *liste;
    liste = (Freunde*) malloc(sizeof(Freunde));

    liste->Name = "Mert";
    liste->next = NULL;    

    add(liste, "Thomas");
    add(liste, "Markus");
    add(liste, "Hanko");

    Ausgabe(liste);

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

c struct pointers

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

指向const int但仍然修改数据的指针

#include<stdio.h>
#include<stdlib.h>
int *func(int *);
int main(void)
{
        int i,size;
        const int *arr=func(&size);
        for(i=0;i<size;i++)
        {
                printf("Enter a[%d] : ",i);
                scanf("%d",&arr[i]);
        }

        for(i=0;i<size;i++)
        {
                printf("%d\t",arr[i]);
        }
        return 0;
}
int *func(int *psize)
{
        int *p;
        printf("Enter the size: ");
        scanf("%d",psize);

        p=(int *)malloc(*psize *sizeof(int));
        return p;
}
Run Code Online (Sandbox Code Playgroud)

输入大小:3

输入[0]:1

输入[1]:2

输入[2]:3

1 2 3

  • 这里,在这段代码中,我使用const关键字不修改'arr'指针指向的数据.
  • 如果我使用const关键字,为什么它给我输出?

c malloc pointers const

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

(*)[]和*[]声明之间的区别

编辑
我已经在这里阅读了问题和答案:指向数组/指针数组的C指针消歧.那里的答案并没有解决我的问题的重点以及下面已经发布的一些答案.这是类似的,但不是重复的.这里的重点旨在集中解释如何()更改声明.链接的重点更广泛,实际上并不围绕()声明的影响和影响,例如我在下面列出的声明.

造成差异的原因是什么?

  1. int *a[5];

  2. int (*a)[5];

被创造了吗?

我对如何()更改声明的解释特别感兴趣.

例如:

随着1)

a[0] = malloc(sizeof(int)); //compiles
Run Code Online (Sandbox Code Playgroud)

但是2)它没有.(错误:数组类型int [5]不可分配)
问题:
1)()in 的影响是2)什么?
2)为什么宣言2会被用于宣言1?
(也就是说,为什么要使用声明2?)

c arrays pointers

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

遍历LinkedList时的分段错误

我是C++和一般指针的新手,但我似乎无法解释为什么我在两个几乎完全相同的代码之一中出现分段错误.我正在做一个简单的遍历a的任务LinkedList,这是我做的第一种方式:

Node * curNode = head; //Head is the pointer to the head of the LinkedList
while(curNode) {
    curNode = curNode->next;
}
Run Code Online (Sandbox Code Playgroud)

这会产生所需的结果并成功遍历整个LinkedList.但是,这会引发Segmentation fault错误:

Node * curNode = head;
while(curNode->next != NULL) {
    curNode = curNode->next;
}
Run Code Online (Sandbox Code Playgroud)

将条件更改为已转换的布尔值或!= nullptr不执行任何操作,因此条件不会引发true空指针.然而,我似乎无法弄清楚为什么我得到一个Segmentation fault,似乎适当的循环条件确保我不访问任何空指针.

c++ pointers linked-list

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

为什么将字符串的scanf保存为str而对于int它被发送到i的指针?

我有这个代码:

char str [80];
int i;
printf ("Enter your family name: ");
scanf ("%79s",str);  
printf ("Enter your age: ");
scanf ("%d",&i);
Run Code Online (Sandbox Code Playgroud)

为什么scanf()string保存str而对于int它被发送到pointer of i

c arrays pointers scanf

0
推荐指数
2
解决办法
127
查看次数

如何将0分配给变量'a'?

我用C写了一个简单的程序(C中的新手)

当我尝试编译这个程序时,它会被编译.

创建指向int'p'的指针并存储变量'a'的地址

当我derefernce指针它显示值0但我从未分配任何值

到我的变量那么0来自哪里?如果我们写 int a; 它,它甚至在为它分配任何值之前在内存中分配空间.

#include <stdio.h>

int main()
{
    int a;
    int *p = &a;
    printf("%d\n", *p);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器:clang

c pointers

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

标签 统计

pointers ×10

c ×6

c++ ×4

arrays ×2

const ×1

function ×1

linked-list ×1

malloc ×1

scanf ×1

string ×1

struct ×1