I'm doing an assignment where we have to read a series of strings from a file into an array. I have to call a cipher algorithm on the array (cipher transposes 2D arrays). So, at first I put all the information from the file into a 2D array, but I had a lot of trouble with conflicting types in the rest of my code (specifically trying to set char[] to char*). So, I decided to switch to an array of pointers, which made everything a lot easier in most of my code.
But now I need to convert char* to char[] and back again, but I can't figure it out. I haven't been able to find anything on google. I'm starting to wonder if it's even possible.
Software Engineering C C uses char type to store characters and letters. However, the char type is integer type because underneath C stores integer numbers instead of characters.In C, char values are stored in 1 byte in memory,and value range from -128 to 127 or 0 to 255.
If type of array is 'char' then it means the array stores character elements. Since each character occupies one byte so elements of a character array occupy one byte each.
Difference between char s[] and char *s in C There are some differences. The s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.
Use the valueOf() method in Java to copy char array to string. You can also use the copyValueOf() method, which represents the character sequence in the array specified. Here, you can specify the part of array to be copied.
It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char *
and char []
) are not the same thing.
char a[SIZE]
says that the value at the location of a
is an array of length SIZE
char *a;
says that the value at the location of a
is a pointer to a char
. This can be combined with pointer arithmetic to behave like an array (eg, a[10]
is 10 entries past wherever a
points)In memory, it looks like this (example taken from the FAQ):
char a[] = "hello"; // array +---+---+---+---+---+---+ a: | h | e | l | l | o |\0 | +---+---+---+---+---+---+ char *p = "world"; // pointer +-----+ +---+---+---+---+---+---+ p: | *======> | w | o | r | l | d |\0 | +-----+ +---+---+---+---+---+---+
It's easy to be confused about the difference between pointers and arrays, because in many cases, an array reference "decays" to a pointer to it's first element. This means that in many cases (such as when passed to a function call) arrays become pointers. If you'd like to know more, this section of the C FAQ describes the differences in detail.
One major practical difference is that the compiler knows how long an array is. Using the examples above:
char a[] = "hello"; char *p = "world"; sizeof(a); // 6 - one byte for each character in the string, // one for the '\0' terminator sizeof(p); // whatever the size of the pointer is // probably 4 or 8 on most machines (depending on whether it's a // 32 or 64 bit machine)
Without seeing your code, it's hard to recommend the best course of action, but I suspect changing to use pointers everywhere will solve the problems you're currently having. Take note that now:
You will need to initialise memory wherever the arrays used to be. Eg, char a[10];
will become char *a = malloc(10 * sizeof(char));
, followed by a check that a != NULL
. Note that you don't actually need to say sizeof(char)
in this case, because sizeof(char)
is defined to be 1. I left it in for completeness.
Anywhere you previously had sizeof(a)
for array length will need to be replaced by the length of the memory you allocated (if you're using strings, you could use strlen()
, which counts up to the '\0'
).
You will need a make a corresponding call to free()
for each call to malloc()
. This tells the computer you are done using the memory you asked for with malloc()
. If your pointer is a
, just write free(a);
at a point in the code where you know you no longer need whatever a
points to.
As another answer pointed out, if you want to get the address of the start of an array, you can use:
char* p = &a[0]
You can read this as "char pointer p
becomes the address of element [0]
of a
".
If you have char[] c
then you can do char* d = &c[0]
and access element c[1] by doing *(d+1)
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With