标签: pointers

指针增量时的行为.C++

请考虑以下代码:

int main()
{
    int* p = new int(3);
    p+=4;
    std::cout<<*p<<std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我的编译器(Visual Studio 2012)打印:-7514522142int这种情况.
那么我们能以某种方式推断出输出并且这段代码合法吗?

c++ pointers dereference

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

程序打印垃圾而不是函数调用的实际返回值

我写了一个算法来从字符串中提取单词并将它们存储在一个数组中,但是我得到了一个不需要的结果,我无法弄清楚为什么.输出是一个非常奇怪的字符.

#include <stdio.h>
#include <windows.h>

char *extract(char *String)
{
char str[10][20];
int x = -1,y = 0,z = 0;
for( size_t n = 0 ; n < strlen(String) ; n++ )
{
    y++;
    if( String[n] == ' ' ) y = 0, z = 0;

    if( y == 1 ) x++;

    if( y > 0 )
    {
        str[x][z] = String[n];
        z++;
        str[x][z] = '\0'; //Comment this*********
    }
}

/*for( int n = 0; n < 10; n++ ) Uncomment this***** …
Run Code Online (Sandbox Code Playgroud)

c printf pointers function return-value

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

与char**混淆

如果我写

//case 1
char *animals[2] = {"cat", "dog"};
char **animal_ptr = animals; 
printf("%s\n", *(animal_ptr + 1)); //fine
Run Code Online (Sandbox Code Playgroud)

而且,以不同的方式:

//case 2
char *animal = "cat";
char **animal_ptr = &animal;
 *(animal_ptr + 1) = "dog";
printf("%s\n", *(animal_ptr + 1)); //fine
Run Code Online (Sandbox Code Playgroud)

所以,我对上面的两个例子感到困惑.

  1. 在案例1中,我理解这animal_ptr是一个指向指针集合的指针,并且由于指针保存地址,我不需要添加&.但是在第2种情况下,我必须添加一个&for才能工作,即使animal它已经是一个指针.为什么?

  2. 在这两种情况下,为什么通过另一个可接受的指针修改字符串文字?据我所知,当你声明一个像char *x = "..etc";这样的字符串时,它被放在一个无法修改的内存中.那么,为什么在情况1,无论animalanimal_ptr可修改字符串?

  3. 为什么strcpy(*(animal_ptr + 1), "bird")失败,程序停止,即使任务在2中工作.?

  4. 在案例2中:

    • 当我这样做printf("%s", *animal_ptr),它工作正常,对我有意义.
    • 当我这样做时printf("%s", *animal),它会停止.为什么?

谢谢,很抱歉很多问题.

c arrays pointers char

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

初始化动态分配的数组后出现malloc()错误

我试图malloc在初始化另一个动态分配的数组后再次调用,但我的程序无法运行(尽管它可以通过编译).我的部分代码如下.

table = (Node **)malloc(m * sizeof(Node*));

for(i=0; i<=m; i++)
  table[i] = NULL;

table2 = (Node *)malloc(n * sizeof(Node));
Run Code Online (Sandbox Code Playgroud)

错误信息如下:

malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)
->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_si
ze == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (st
ruct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t
))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' 
failed.
Run Code Online (Sandbox Code Playgroud)

最奇怪的是,我发现我的程序在删除上面代码中的第二行和第三行后可以成功运行,其中 …

c malloc pointers dynamic-allocation

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

两个char数组C的连接

int main(int argc, char *argv[]) {
  char *s = argv[1];
  *(s + (strlen(argv[1]))) = argv[2];
  printf("%s \n", s);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我跑步时:./concat hello, world输出是:
hello,Mworld我期待
hello,world

什么是M炭?为什么C把这个?

c arrays string pointers char

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

指向数组的指针,为什么这是最后一个元素的地址?

我正在使用教程point.com示例.

int  var[MAX] = {10, 100, 200};
int  *ptr;

// let us have address of the last element in pointer.
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{
    cout << "Address of var[" << i << "] = ";
    cout << ptr << endl;

    cout << "Value of var[" << i << "] = ";
    cout << *ptr << endl;

    // point to the previous location
    ptr--;
}
return 0;
Run Code Online (Sandbox Code Playgroud)

那么,&var[MAX - 1] 为什么不 …

c++ pointers

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

使用指针C写入文件/从文件读取

我编写了一个程序,将指针写入文件(fwrite)并从文件读取指针(fread)。但是,该程序似乎没有在文件中写入任何内容,也没有从文件中读取任何内容。它只是将指针的最终增量打印5次并退出。谁能在我的语法中发现似乎正在执行此操作的错误/错误?

#include <stdio.h>

int main() {
    FILE *fTest;
    int *testPtr;
    int x = 10;
    
    if ((fTest = fopen("test.c", "wb")) == NULL) {
        printf("Error!");
    }

    testPtr = &x;
    int i;
    for (i = 0; i < 5; i++) {
        fwrite(testPtr, sizeof(int), 1, fTest);
        *testPtr += 1;
    }
    
    for (i = 0; i < 5; i++) {
        fread(testPtr, sizeof(int), 1, fTest);
        printf("%d", *testPtr);
    }

    fclose(fTest);
}
Run Code Online (Sandbox Code Playgroud)

c pointers file fwrite fread

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

C++ - 在vector(或其他容器)构造函数中使用期望类型为Iterator的数组/指针.怎么可能?

下面的文字来自C++在线课程.它说的是vector类的构造函数

 template <class InputIterator>
          vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
Run Code Online (Sandbox Code Playgroud)

可以接收指针作为first(InputIterator first)和第二个参数(InputIterator last).

下一个构造函数使用迭代器来初始化自身.它将创建一个带有从第一个(包括)到最后一个(不包括)的值副本的向量.在最典型的情况下,此构造函数使用现有集合中的元素创建新向量.但是由于迭代器被定义为一组操作而不是某种类型,因此也可以使用普通指针.这句话得出的结论是,您可以使用普通的C++数组来初始化集合.事实上,你可以.

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    int a1[]={1,2,3,4,5,6,7,8,9,10};
    //first one
    vector<int> v1(a1, a1+10);
    cout<<"Size (v1):  "<<v1.size()<<endl;
    for(unsigned i = 0; i < v1.size(); ++i)
    {
        cout<< v1[i]<<" ";
    }
    cout<<endl;
    //second one;
    vector<int> v2(a1+5,a1+10);
    cout<<"Size (v2):  "<<v2.size()<<endl;
    for(unsigned i = 0; i < v2.size(); ++i)
    {
        cout<< v2[i]<<" ";
    }
    cout<<endl; …
Run Code Online (Sandbox Code Playgroud)

c++ arrays pointers vector

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

C中的指针与数组类似,但指针更有用

为什么指针可以自由地用作数组呢?我认为指针在内存操作中更强大和重要吗?有人可以向我解释指针在哪种情况下更重要吗?

这是我的代码:

char * myString ="This is my String";
while(*myString){
  putchar(*myString++);
}
puts("\n");

char * myString1 ="This is my String";
int j=0;
while(myString1[j]!='\0'){
  putchar(myString1[j++]);
}
Run Code Online (Sandbox Code Playgroud)

他们打印相同的结果.

c pointers

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

在c中用箭头符号打印结构元素

我有一个带指针的结构..我只是在徘徊如何用指针打印结构的值,所以我写了一个代码像

#include <stdio.h>
#include <string.h>

typedef struct Books
{
   char  title[0];
} Book,*andi;


int main( )
{
   Book book;
   Book *andi;
   strcpy(book.title, "avi");
   printf( "Book title : %s\n", andi->title);
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我尝试这个代码它没有给我一个错误,但产生一个空结果而不是"avi"..你能告诉我为什么会发生这种情况??

任何帮助都会很棒..谢谢

c struct pointers

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