tipos sirven que punteros puntero para los ejemplos declaracion cadenas arreglos aritmetica c string pointers split

sirven - que es un puntero en c++



¿Por qué al apuntador de caracteres solo se le asigna un carácter, mientras que se supone que se le debe asignar una cadena? (1)

Esta línea es incorrecta:

arr=malloc(N*sizeof **arr);

debería ser:

arr=malloc(N*sizeof *arr);

**arr es un char , por lo que solo asigna espacio para N bytes, pero necesita espacio para N punteros, que son 4 bytes. Entonces, no estás asignando suficiente espacio, y luego estás escribiendo fuera de los límites de la matriz, lo que da como resultado un comportamiento indefinido.

MANIFESTACIÓN

Una cadena contiene tres partes separadas por espacio. Las dos primeras partes son cadenas y la tercera parte es un número entero.

La salida del siguiente programa me sorprende.

#include <stdio.h> #include <string.h> #include <stdlib.h> void split(char *ch,char **part1,char **part2,int *num){ int length=strlen(ch), *lc1,*lc2; int i=0,j=0; printf("value of length is %d/n",length); printf("this function recieved %s for splitting into pieces/n",ch); lc1=(char *)malloc(length*sizeof(char)); lc2=(char *)malloc(length*sizeof(char)); while(ch[i]!='' ''){ lc1[i]=ch[i]; printf("/nin loop with lc1[i] = %c and ch[i] = %c",lc1[i],ch[i]); i++; } lc1[i]=''/0''; i++; while(ch[i]!='' ''){ lc2[j]=ch[i]; printf("/nin loop with lc2[j] = %c and ch[i] = %c",lc2[j],ch[i]); j++; i++; } lc2[j]=''/0''; i++; *num=atoi(&ch[i]); *part1=lc1; *part2=lc2; printf("/nsplit results are:/n"); printf("part1=%s and part2=%s and num=%d and lc1=%s lc2=%s //this is surprising me",*part1,*part2,*num,lc1,lc2); } int main() { int N,i,j,n,*numArray,count=0; char **arr,*part1,*part2,*token; scanf("%d",&N); arr=malloc(N*sizeof **arr); numArray=malloc(N*sizeof *numArray); for(i=0;i<N;i++){ arr[i]=(char *)malloc(50*sizeof(char)); } for(i=0;i<N;i++){ printf("plz enter %d th :",i); scanf(" "); gets(&arr[i][0]); } for(i=0;i<N;i++){ printf("%s",arr[i]); } for(i=0;i<N;i++){ /*token=strtok(arr[i]," "); part1=token; token=strtok(NULL," "); part2=token; token=strtok(NULL," "); n=atoi(token);*/ split(arr[i],&part1,&part2,&n); //some logic to use part1 and part2 of the sentence } return 0; }

La entrada que proporcioné es la siguiente:

1 abcd efgh 2

La primera línea de la entrada contiene el número de oraciones que quiero seguir. La salida que obtuve es la siguiente:

plz enter 0 th :abcd efgh 2value of length is 11 this function recieved abcd efgh 2 for splitting into pieces in loop with lc1[i] = a and ch[i] = a in loop with lc1[i] = b and ch[i] = b in loop with lc1[i] = c and ch[i] = c in loop with lc1[i] = d and ch[i] = d in loop with lc2[j] = e and ch[i] = e in loop with lc2[j] = f and ch[i] = f in loop with lc2[j] = g and ch[i] = g in loop with lc2[j] = h and ch[i] = h split results are: part1=a and part2=e and num=2 and lc1=a lc2=e //this is surprising me Success time: 0 memory: 2296 signal:0 plz enter 0 th :abcd efgh 2value of length is 11 this function recieved abcd efgh 2 for splitting into pieces in loop with lc1[i] = a and ch[i] = a in loop with lc1[i] = b and ch[i] = b in loop with lc1[i] = c and ch[i] = c in loop with lc1[i] = d and ch[i] = d in loop with lc2[j] = e and ch[i] = e in loop with lc2[j] = f and ch[i] = f in loop with lc2[j] = g and ch[i] = g in loop with lc2[j] = h and ch[i] = h split results are: part1=a and part2=e and num=2 and lc1=a lc2=e //this is surprising me

Aunque la función de bucle dentro de la división () se está ejecutando con éxito para todos los caracteres de la cadena, aún estoy obteniendo solo un carácter en la parte1 (también en lc1) y en la parte2 (también en lc2). ¿Cuál es el motivo de esta salida?