-1

I can't seem to change individual characters in a pointer without the program crashing. R1 must be a pointer.

char * R1[6];       
int main (){
   *R1 = "33333";
   printf("%s\n", *R1);
   R1[3] = '4';
   printf("%s", *R1);
   return 0;
}
  • possible duplicate of [What is the difference between char s\[\] and char \*s in C?](http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c) – Fernando Jan 05 '15 at 23:16

3 Answers3

3

This should work for you:

#include <stdio.h>

char  R1[6] = "33333";

int main () {

    printf("%s\n", R1);
    R1[3] = '4';
    printf("%s", R1);

    return 0;

}

Output:

33333
33343

EDIT:

With a pointer this should work for you:

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

int main () {

    char * R1 = malloc(6*sizeof(char));
    strcpy(R1, "33333");

    printf("%s\n", R1);
    R1[3] = '4';
    printf("%s", R1);

    free(R1);

    return 0;

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • @34hjjekjr: You can just take a pointer to the array if you don't want to/aren't allowed to use dynamic allocation. – Ed S. Jan 05 '15 at 23:07
  • Thanks, this works although I might have to change my actual program. –  Jan 05 '15 at 23:31
1

Your code must be like this to get it work

char  R1[6]="33333";       
int main (){
   printf("%s\n", R1);
   R1[3] = '4';
   printf("%s", R1);
   return 0;
}

let's analyze your wrong code to figure out the problems

first ERROR

char * R1[6];  

here you are declaring an array of 6 pointer pointing each one to a char but you want an array of 6 char that's why it should be char R1[6]; to get what you want

second ERROR

   *R1 = "33333";

in C language you cannot assign a string by using the operator = . You have two choices either you initialize when declaring it like that char R1[6] = "33333"; or you can use the function strcpy() located in the string.h library and it wil be strcpy(R1,"33333");

third ERROR:

printf("%s\n", *R1);

when dereferencing a string in printf() you need to provide only the address of the first element of the string which is the name of the array R1 that's why it should be like that the instruction

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

the same thing for the last printf()

1

The variable you are declaring is not a pointer; it is in fact an array of six char pointers (this is why printf fails). If you want to use a char array, simply write:

char R1[6];

If you want a char pointer, write:

char *R1;

And initialize it somehow (probably with malloc):

/*in main*/
R1 = malloc(6); /*six bytes for five chars +null terminator*/

Make sure to zero-terminate your string:

R1[5] = '\0';

Once you have a valid, mutable* char sequence, you can assign to individual chars much like you are already:

R1[3] = '4';

However, when it comes to assigning the whole string, you'll want to use the strcpy function instead of the = operator:

/*R1 now contains the text "33333"*/
strcpy(R1, "33333");

*When you write R1 = "33333", you are assigning to the pointer instead of to the text it points to. Since "33333" is immutable, you won't be able to assign to the values in your string when your string is a direct reference to it.

So the complete program might look like this:

char *R1;       
int main (){
   R1 = malloc(6);
   R1[5] = '\0';
   strcpy(R1, "33333");
   printf("%s\n", R1);
   R1[3] = '4';
   printf("%s", R1);
   free(R1);
   return 0;
}
ApproachingDarknessFish
  • 14,133
  • 7
  • 40
  • 79
  • 2
    @Meninx That's a matter of taste. The standard mandates that `sizeof(char) == 1` so it's perfectly safe to just use the number of bytes you want, but your way is also fine, if a little verbose. Strictly speaking, the "best" way to allocate would probably be `malloc(6*sizeof(*R1))`, which guarantees that the correct amount of memory is allocated even if we change the type of R1. – ApproachingDarknessFish Jan 05 '15 at 23:25