¿Cómo puedo asignar una matriz bidimensional a otra matriz bidimensional temporal…? en Programación C

Hi I am trying to store the contents of two dimensional array into a temporary array.... How is it possible... I don't want looping over here, as it would add an extra overhead.. Any pointer notation would be good.

struct bucket
{
   int nStrings;       
   char strings[MAXSTRINGS][MAXWORDLENGTH];
};

void func()
{
   char **tArray;
   int tLenArray = 0;
   for(i=0; i<TOTBUCKETS-1; i++)
   {
      if(buck[i].nStrings != 0)
      {
         tArray = buck[i].strings;
         tLenArray = buck[i].nStrings;
      }
   }
}

The error here i am getting is:-
[others@centos htdocs]$ gcc lexorder.c
lexorder.c: In function âlexSortingâ:
lexorder.c:40: warning: assignment from incompatible pointer type

Please let me know if this needs some more explanaition...

preguntado el 09 de enero de 11 a las 05:01

3 Respuestas

Puedes usar memcpy o envuélvelo en un struct and use assignment on the structs, but any way you do it, you'll effectively be looping over all the elements. That's fundamental.

Respondido el 09 de enero de 11 a las 08:01

The compiler issue is that the notation array[...][...] is not the same as char **. The char ** notation is pointer to a pointer.

Por ejemplo;

char ** pA;
pA = malloc(10);

pA[0] is a pointer

in the case of array[...][...]

array[0] is not a pointer. It is a char.

If you want to do what you are trying then you you need declare strings as char ** and initialize your structure like this.

struct bucket
{
    int nStrings;       
    char** strings;
};

struct bucket buck;

buck.strings = (char **)malloc(MAXSTRINGS);

for(i = 0; i < MAXSTRINGS; i++)
    buck.strings[i] = (char*)malloc(MAXWORDLENGTH)

Respondido el 09 de enero de 11 a las 10:01

You mean cloning the array right?
Unfortunately, I don't think there is a way to clone an array in C without looping through all items in the array and making a copy of them somewhere else.
The main problem is that since they are pointers, any thing you do to simply copy the contents would just copy the addresses and you would have a reference, not a clone.

Respondido el 09 de enero de 11 a las 18:01

bucket is a solid block of memory usable with memcpy - please delete me

this is a C question, not C++ - Jens Gustedt

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.