为什么我需要使用类型**指向键入*? [英] Why do I need to use type** to point to type*?

查看:82
本文介绍了为什么我需要使用类型**指向键入*?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读学习C艰难的历程了几天,但这里的东西我想真正了解。捷思锐,作者写道,的char ** 是一个指针(字符指针),并说,这是必要的,因为我想点的东西2维

下面是正是写在网页


  

一个char *已经是字符指针,所以这只是一个字符串。然而,你需要2级,因为名字是二维的,这意味着你需要的char **的指针(字符指针)的类型。


这是否意味着我必须使用,可以指向的东西2维的,这是一个变量,为什么我需要两个 **

只是一个小跟进,这是否也适用于n维?

下面是有关的code

 的char *名称[] = {阿兰,弗兰克,玛丽,约翰,丽莎};
焦炭** cur_name =名称;


解决方案

没有,该教程是质量问题。我不建议继续阅读。

A 的char ** 是一个指针到指针。它不是一个二维数组。
它不是一个指针阵列。
这不是一个指向一个二维数组。

本教程的作者可能是困惑,因为有广泛-S $ P $垫坏和不正确的做法说你应该动态分配二维数组是这样的:

  // BAD!不要做这样的!
INT ** heap_fiasco;
heap_fiasco =的malloc(X *的sizeof(INT *));
为(中间体X = 0; X&所述; X; X ++)
{
  heap_fiasco [X] =的malloc(Y *的sizeof(INT));
}

这是不是然而一个二维数组,它是分配全堆一个缓慢的,零碎的查找表。在查找表中访问一个项目的语法, heap_fiasco [X] [Y] ,看起来就像数组索引的语法,因此,因此很多人出于某种原因认为,这你是怎么分配的二维数组。

动态分配一个二维数组,正确的方法是:

  //正确
INT(* array2d)[Y] =的malloc(sizeof的(INT [X] [Y]));

您可以告诉大家,首先是不是一个数组,因为如果你这样做的memcpy(heap_fiasco,heap_fiasco2,X * Y)的code就会和好如初。该项目未在相邻的存储器分配

同样的memcpy(heap_fiasco,heap_fiasco2,sizeof的(* heap_fiasco))也和好如初,但对其他方面的原因:你得到一个指针不是数组的大小

的memcpy(array2d,array2d_2,sizeof的(* array2d))将工作,因为它是一个二维数组。

I've been reading Learn C The Hard Way for a few days, but here's something I want to really understand. Zed, the author, wrote that char ** is for a "pointer to (a pointer to char)", and saying that this is needed because I'm trying to point to something 2-dimensional.

Here is what's exactly written in the webpage

A char * is already a "pointer to char", so that's just a string. You however need 2 levels, since names is 2-dimensional, that means you need char ** for a "pointer to (a pointer to char)" type.

Does this mean that I have to use a variable that can point to something 2-dimensional, which is why I need two **?

Just a little follow-up, does this also apply for n dimension?

Here's the relevant code

char *names[] = { "Alan", "Frank", "Mary", "John", "Lisa" };
char **cur_name = names;

解决方案

No, that tutorial is of questionable quality. I wouldn't recommend to continue reading it.

A char** is a pointer-to-pointer. It is not a 2D array. It is not a pointer to an array. It is not a pointer to a 2D array.

The author of the tutorial is likely confused because there is a wide-spread bad and incorrect practice saying that you should allocate dynamic 2D arrays like this:

// BAD! Do not do like this!
int** heap_fiasco;
heap_fiasco = malloc(X * sizeof(int*));
for(int x=0; x<X; x++)
{
  heap_fiasco[x] = malloc(Y * sizeof(int));
}

This is however not a 2D array, it is a slow, fragmented lookup table allocated all over the heap. The syntax of accessing one item in the lookup table, heap_fiasco[x][y], looks just like array indexing syntax, so therefore a lot of people for some reason believe this is how you allocate 2D arrays.

The correct way to allocate a 2D array dynamically is:

// correct
int (*array2d)[Y] = malloc(sizeof(int[X][Y]));

You can tell that the first is not an array because if you do memcpy(heap_fiasco, heap_fiasco2, X*Y) the code will crash and burn. The items are not allocated in adjacent memory.

Similarly memcpy(heap_fiasco, heap_fiasco2, sizeof(*heap_fiasco)) will also crash and burn, but for other reasons: you get the size of a pointer not an array.

While memcpy(array2d, array2d_2, sizeof(*array2d)) will work, because it is a 2D array.

这篇关于为什么我需要使用类型**指向键入*?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆