Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to convert char[] to char* in C?

Tags:

c

char

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.

like image 691
tparf Avatar asked Mar 09 '12 02:03

tparf


People also ask

What does char [] do in C?

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.

What is a char * array in C?

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.

Is a char * the same as char in C?

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.

Can you convert a char array to string?

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.


2 Answers

It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char * and char []) are not the same thing.

  • An array char a[SIZE] says that the value at the location of a is an array of length SIZE
  • A pointer 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".

like image 74
Timothy Jones Avatar answered Oct 20 '22 08:10

Timothy Jones


If you have char[] c then you can do char* d = &c[0] and access element c[1] by doing *(d+1), etc.

like image 40
dave Avatar answered Oct 20 '22 06:10

dave