Assign a variable array of characters to a new variable [duplicate]

0

I'm trying to assign the array to another variable to get one of the specific characters, by a pointer

int main(){
     char id[10];
     char *idn[10];
     FILE *in;
     fscanf(in,"%s",id);

     idn[10]=&id[10];
     printf("%s\n",*idn);
 return 0;
    
asked by Jorge Ignacio Martinez-abarca 17.04.2016 в 02:53
source

1 answer

1

If what you want is to get a character from an array to use it from idn you can try something like this:

#include <stdio.h>

int main(void) {

     char id[10] = {'h','o','l','a'};
     char *idn;

     //FILE *in;
     //fscanf(in,"%s",id);

     idn = &id[0];   //<- tomas la posicion que quieres en este caso la 0

     printf("%c\n",*idn); //<- usas como formato %c

     printf("%s\n",id);

    return 0;
}

Test-ideone

If you want to do something similar to this arr = arr1 you can use snprintf .

#include <stdio.h>

int main(void) {

    char id[10] = {'h','o','l','a'};
    char idn[10];
    //FILE *in;
    //fscanf(in,"%s",id);

    size_t des_size = sizeof (idn);

    snprintf(idn, des_size, "%s", id);

    printf("%s\n",idn);

    return 0;
}

link

Test-ideone

You could also use strncpy(idn, id, des_size); but you have to take into account the following that the documentation says:

  

No null-character is implicitly appended at the end of destination if   source is longer than num Thus, in this case, destination shall not   be considered a null terminated C string (reading it as such would   overflow).

In the link you can see an example of how to handle this situation.

link

    
answered by 17.04.2016 в 12:53