I'm trying to copy 3 strings from 2D const char array into a double pointer of char.
char **E = malloc(sizeof(char));
const char *e[3] = {"aa", "bb", "cc"};
for (int k=0; k<3; k++)
{
E[k] = malloc(sizeof(char));
strcpy(E[k], e[k]); //crash occurs here
}
This solution is when you want E to point to the same constant strings
as the variable e.
Try this code:
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
char **E = malloc(3*sizeof(char *));
const char *e[3] = {"aa", "bb", "cc"};
int k;
for (k=0; k<3; k++)
{
E[k] = e[k];
}
for( k=0; k<3; k++ )
{
printf( "%s\n", E[k]);
}
return 0;
}
Here is the explanation.
The variable 'e' is an array of 3 pointers to constant characters.
Lets say "aa" is stored in memory address of 100.
Lets say "bb" is stored in memory address of 200.
Lets say "cc" is stored in memory address of 300.
The 'e' variable looks like this:
e[0] e[1] e[2]
|------|-------|------|
| 100 | 200 | 300 |
|------|-------|------|
The E[k] = e[k]; assignment would result E to look like
E[0] E[1] E[2]
|------|-------|------|
| 100 | 200 | 300 |
|------|-------|------|
char **E = malloc(sizeof(char));
A pointer to pointer to char must reserve space for a pointer to char, not for a plain char.
It seems that you want a modifiable copy of the constant array of pointers, then change to:
char **E = malloc(sizeof(char *) * 3);
const char *e[3] = {"aa", "bb", "cc"};
for (int k=0; k<3; k++)
{
E[k] = malloc(strlen(e[k]) + 1);
strcpy(E[k], e[k]);
}
char **E = malloc(sizeof(char));
should be
char **E = malloc(3 * sizeof(char*));
The former allocates just one byte but you need to allocate memory for 3 pointers to char (char*) and this is what the latter does.
Similarly,
E[k] = malloc(sizeof(char));
should be
E[k] = malloc(strlen(e[k]) + 1);
as you need memory to store each of the respective strings (+1 for the NUL-terminator '\0') in E[K] and not just 1 byte.
E is a char**, which means that it contains char* that themselves contain char.
This mean that E have to be allocated enough space to contain the char*, that mean malloc of:
sizeof(char*) * thenumberofchar*youwantittocontains
Same goes for each char* (E[k]) contained into E, they have to be allocated of
sizeof(char) * thenumberofcharyouwanittocontains + 1 (for the '/0' character that tell that the char* has ended)
Because if you don't allocate enough memory space, you won't be able to write at all memory location you're trying to access via E[k] and even E[k][i], because it's not allocated for your program, causing a Segmentation Fault.
Related
I am given an array of strings in the form of a pointer to a pointer of a character (that is, char **). I wish to access each element in the array,
char **strings = {"hello", "bye", "whatusp"};
printf("%s\n", *(&strings));
printf("%s\n", *(&strings + 1));
I notice that the first printf() works, as it outputs "hello" for me, but the second doesn't output anything. The +1 does increment by 8 instead of 1, since the address is in bytes. How can I access an arbitrary element in such a double pointer?
Just as simple as:
char *strings[] = {"hello", "bye", "whatusp"};
printf("%s\n", strings[0]);
printf("%s\n", strings[1]);
printf("%s\n", strings[2]);
First of all, the line
char **strings = {"hello", "bye", "whatusp"};
is wrong. If you want to declare an array of 3 pointers to string literals, then it should be the following instead:
char *strings[3] = {"hello", "bye", "whatusp"};
In the previous line, you can omit the 3, because the compiler is smart enough to count the number of elements itself, since you are initializing the array.
The lines
printf("%s\n", *(&strings));
printf("%s\n", *(&strings + 1));
are also wrong, because when using printf with the %s format specifier, you must provide a pointer to a string, i.e. a char *. However, you are providing a char** instead in both lines.
The expression *(&strings) will evaluate to a char ** for the following reason:
The expression &strings will evaluate to a pointer to strings. Since strings itself is an array of 3 pointers to char, a pointer to strings is a pointer to an array of 3 pointers to char. Afterwards, you are applying the indirection operator * on this data type, so that the result of this operation is an array of 3 pointers to char. When passing this data type to printf, the array will decay to a pointer to the first element of the array, i.e. to a pointer to a pointer to a char (which is a char**).
You should simply write the following instead:
printf( "%s\n", strings[0] );
printf( "%s\n", strings[1] );
The expression strings[1] is equivalent to *(strings+1).
char **strings is a pointer to a pointer to char, not array of pointers to char, your compiler should have given you a warning anyways, if not you should enable them.
For that assignment to work, you will need to have an array of pointers, char *strings[] or if you want to use plain pointer notation (array notation is using pointer arithmetic in the background though), you could dynamically allocate memory for the first dimension.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
int main(void)
{
char **strs = NULL;
if ((strs = malloc(SIZE * sizeof(char *))) == NULL) /* check malloc result */
{
exit(EXIT_FAILURE);
}
*strs = "hello"; /* assignment makes sense in this case but you cant modify the string since its read-only*/
*(strs + 1) = "bye";
*(strs + 2) = "whatsup";
for (int i = 0; i < SIZE; i++)
{
puts(*(strs + i));
}
/* Alternative that will allocate space for each string if you need to store them or modify them later */
/* for (int i = 0; i < 3; i++)
{
if ((*(strs + i) = malloc(YOUR_STRINGS_LENGTH)) == NULL)
{
exit(EXIT_FAILURE);
}
snprintf(*(strs + i), YOUR_STRINGS_LENGTH, "string");
}
*/
return 0;
}
I am given an array of strings in the form of a pointer to a pointer of a character (that is, char **)
There is no such thing: a char** is just a pointer. It is not an array.
if thing is char** then
*thing is char* and **thing is, as declared, a char.
An array can be seen as a pointer, but a pointer is not an array.
note: the prototype for main is in general
int main( int argc, char** argv)
so what you need to do is the same as every C program gets from the system.
Example
To have char** as an array of pointers to char you must build it
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char* array[] = {
"I", "wish", "to", "access", "each", "element",
"in", "the", "array"
};
int N = sizeof(array) / sizeof(char*);
printf("%d strings in total\n\n", N);
char** strings = (char**)malloc(N * sizeof(char*));
for (int i = 0; i < N; i=i+1)
*(strings+i) = *(array + i);
for (int i = 0; i < N; i=i+1)
printf("%3d %s\n", 1 + i, *(i + strings));
// pointer is not an array
char thing = '#';
char* p = &thing;
char** pp = &p;
printf("\n\nthing is '%c', *p is '%c', **pp is '%c'\n",
thing, *p, **pp);
return 0;
}
output
9 strings in total
1 I
2 wish
3 to
4 access
5 each
6 element
7 in
8 the
9 array
thing is '#', *p is '#', **pp is '#'
In particular note this lines
char** strings = (char**)malloc(N * sizeof(char*));
for (int i = 0; i < N; i=i+1)
*(strings+i) = *(array + i);
where is defined that strings is an area of the needed size and than the pointers are initialized to the strings of the input array.
HI I'm getting a segfault with this code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **array;
int i = 0;
array = (char **)malloc(sizeof(char *) * 20);
for (i = 0; i < 20; i++)
{
array[i] = (char *)malloc(sizeof(char *) * 20);
array[i] = "____________________";
}
array[0][0] = '2';
return(0);
}
What am I doing wrong ?
I'm very confused when I need to modify 2d arrays, what are my best options here ?
Thanks a lot.
You get a segfault because with array[i] = "____________________"; you let point each array[i] to a string literal. Note that you do not copy the contents of the literal but let array[i] directly point to it. Consequently, with array[0][0] = '2' you are altering a string literal then, which is undefined behaviour (very likely to become apparent as a segfault in this case).
Use strcpy(array[i],"____________________); instead.
BTW: write array[i] = malloc(sizeof(char) * (20+1)); (note the sizeof(char) instead of sizeof(char*) here and the +1 for the string termination character).
I'm trying to pass the address of a 2D array to a function in C. I initialize the 2D array as:
const int N = 3;
char t[N][N];
I try to convert this into a char***:
char*** t_ptr = &t;
But it fails with a:
warning: initialization from incompatible pointer type
The function I want to receive the array has a prototype such as:
void f(char*** t, int N) { ... };
What am I doing wrong? Thank you.
This
char*** t_ptr = &t;
is wrong as compiler pointed because t is an two dimensional array, instead of triple pointer like char*** use pointer to an array to point to it. For e.g
char t[3][3] = { "ab","de","gh"}; /* total 3 1D array & each 1D array has 3 elements */
char (*t_ptr)[3] = t; /* t_ptr is a pointer to an array, pointing to 3 elements at a time */
And you can print t_ptr like
for(int index = 0; index < 3 ; index++) {
printf("%s\n",t_ptr[index]);
}
For reading c declarations, you can visit: https://cdecl.org/
Now using #Achal's example array:
#include <stdio.h>
#include <stdlib.h>
int main()
{
// t is a 2D array (not a VLA)
char t[3][3] =
{
{'a', 'b', '\0'},
{'d', 'e', '\0'},
{'g', 'h', '\0'}
};
printf("t is composed of 3 arrays of 3 characters each,\n");
printf("with the following addresses (on my machine):\n");
printf("--------------------------------------------------\n");
printf("%p, %p, %p\n", t[0], t[1], t[2]);
// ------------------------------------------------
// p is an array of 3 char pointers or char*
// Notice initialization
char* p[3] = {t[0], t[1], t[2]};
// following line of code will break, if no '\0' is encountered
// since %s requires a char* and a null terminator
printf("\nPrint strings using p:\n");
printf("------------------------\n");
printf("%s, %s, %s\n", *p, *(p + 1), *(p + 2));
// ------------------------------------------------
// q is a pointer to a char* or q has type char**
// Notice initialization of q (q points to p)
char** q = p;
printf("\nUsing q:\n");
printf("-----------\n");
printf("%s, %s, %s\n", *q, *(q + 1), *(q + 2));
// ---------------- printing characters individually
printf("\nIndividually:\n");
printf("---------------\n");
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
printf("%c ",
*(*(q + i) + j)
);
}
printf("\n");
}
// -----------------------------------------------
// r is a pointer to an array of size 3 containing char
// r advances three char at a time (r doesn't know the size of t)
char (*r)[3] = t; // this is the type of t
printf("\nUsing r:\n");
printf("---------------\n");
printf("%p, %p, %p\n", *r, *(r + 1), *(r + 2));
// to extract chars
printf("%c, %c", *(*(r + 0) + 0), *(*(r + 2) + 1));
// -----------------------------------------------
return EXIT_SUCCESS;
}
Output:
t is composed of 3 arrays of 3 characters each,
with the following addresses (on my machine):
--------------------------------------------------
000000000022FE2F, 000000000022FE32, 000000000022FE35
Print strings using p:
------------------------
ab, de, gh
Using q:
-----------
ab, de, gh
Individually:
---------------
a b
d e
Using r:
---------------
000000000022FE2F, 000000000022FE32, 000000000022FE35
a, h
Process returned 0 (0x0) execution time : -0.000 s
Press any key to continue.
char t[N][N];
is in fact the same as
char t[N * N];
in memory. A pointer to such an array would in both cases be of type char *.
char *** is a pointer to a pointer, that is a pointer to a char, whereas char * is a pointer to a char and that's how you pass array references in C: You pass them as a pointer to the first element of that array and this first element is a char.
C cannot retain the exact type or structure of an array as soon as you pass it to functions. In memory, a char array is just a bunch of memory filled with chars and all you can pass around is a pointer to that memory. If that memory is char [] or char [][] or even char [][][] plays no role, in memory all three are a block full of chars and the function would have to explicitly know the structure in memory, otherwise all char arrays will always be char [] for a function.
I strongly discourage C beginners to ever use multidimensional arrays. Instead of
char t[N][N];
char c = t[y1][x1];
t[y2][x2] = 'X';
use
char t[N];
char c = t[y1 * N + x1];
t[y2 * N + x2] = 'X';
As that's basically what the compiler will internally do anyway.
Note that multidimensional arrays in C are not x-y, but y-x, the first value is the row, the second on the column, please see this tutorial.
Whoever disbelieves what I just said, try out this code:
int main ( ) {
char a[5][5];
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
a[y][x] = x + 10 * y;
}
}
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
printf("%02d ", a[y][x]);
}
printf("\n");
}
printf("------\n");
char * a2 = (char *)a;
for (int y = 0; y < 5; y++) {
for (int x = 0; x < 5; x++) {
printf("%02d ", a2[y * 5 + x]);
}
printf("\n");
}
}
You can run it online, if you like, the output is identical. Also have a look at the assembly code the compiler generates for either loop (e.g. gcc -S) and you will see it's almost identical as even in the first case the compiler uses an add and a mul instruction to access the correct memory location within the array.
This problem is driving me crazy, I'm sure I'm missing something. I need to initialize an array of chars using only pointers. Below is the code I have so far:
int p2(){
/* Implements problem 2 of lab */
// Create an array
char **s = (char**)malloc( 11 *sizeof(char));
char *p = *s;
char start ='A';
while( p != s+10){
*p = start;
start++;
p++;
}
return(0);
}
The problem I'm having is I don't know how to address the characters inside of the array. I understand the base address of the array is **s, and the pointer to the first element is *s. What I don't understand is how to get to **s+10 (i.e. the end of the array).
Can anyone shine some light for me??? Please!
EDIT: Ok, looks like I misunderstood the question. I appears I need to create an array of strings (thus the char ** allocation). Then I need to loop through this array, and assign each string (i.e. char *) a value 15 chars long. Please let me know if I'm understanding this correctly:
char **strings ==> strings[0 ... n ] where each element is a pointer to a char (possibly an array). There for *string ==> strings[0], *(string+1) = strings[1], etc etc.
Am I close or way off?
char **s is 2 dimensional array of characters, or array of C strings if you want.
If you want to use array of characters you should use:
char *string = (char*)malloc( 11 *sizeof(char));
If you really want to initialize array of strings, at first step you're initializing array of pointers, that's:
char **s = (char**)malloc( 11 *sizeof(char *));
Please note that I'm using char * inside sizeof. Than when you may use strings, but at first you must initialize each string.
s[0] = (char*) malloc( 15*size(char)); // This is actually string, 14 characters long (NULL terminated)
char *p = s[0]; // p is pointer to beginning of your string now
And there's two way how to address your string:
s[0][3] // 4th character of your string
p[3]
Or if you want to use just pointers:
char *p = *(s+0);
*(p+3); // 4th character
*((*(s+0))+3) // To do it "hardcore"
EDIT: added an example
When you have **char p and use p++ or p + 1, C increases memory address. *p operator tells compiler that you now want to work with data stored in memory, not with pointer. Therefor those two syntax do the same:
p[10] = 'a';
*(p+10) = 'a';
So if you want traverse both your dimensions, you should use:
for( int i = 0; i < 11; i++){
char *p = *(s+i);
for( int j = 0; j < 10; j++){
*(p + j) = 'a'; // Say you wanna fill them with as
}
// You may also use this syntax:
while( p < (*(s+i) + 10)){ // or use != instead of <
*p = 'a';
p++;
}
}
I think you meant this:
char *s = (char*) malloc(11 *sizeof(char));
char *p = s;
In which case you'd address the characters with s[x]
First, you don't need a char ** unless you need an array of arrays.
Second, you can get to the end of the array with (*s)[10] or *((*s)+10)
Third, in C programming, don't cast the result of malloc()
The code is falling apart here.
char **s = (char**)malloc( 11 *sizeof(char));
You're allocating enough memory for 11 chars, which sounds like what you want to do.
However, you're casting the address to those 11 chars to a (char**) as if you were allocating space for pointers, not chars.
Why should you use a double pointer for creating an array of chars?
char *s = (char *) malloc(10 * sizeof(char));
char *start = s; // save starting pointer
for(; s < start+10; s++)
*s = 'a';
return 0;
If you allocate char ** essentially you're allocating an array of pointers to char(eg. array of strings). If you need char array, allocate char array. If you start working with pointers a design of stack and heap can be really helpful.
AFunc changes what was sent to it, and the printf() outputs the changes:
void AFunc ( char *myStr, int *myNum )
{
*myStr = 's';
*myNum = 9;
}
int main ( int argc, char *argv[] )
{
char someString = 'm';
int n = 6;
AFunc(&someString, &n);
printf("%c" "%d", someString, n);
}
But what if the string was more than one char? How would the code look differently? Thanks for any help.
If it were a "string" instead of a char, you would do something like this:
#include <stdio.h>
void AFunc (char *myStr, int *myNum) {
myStr[0] = 'p'; // or replace the lot with strcpy(myStr, "pax");
myStr[1] = 'a';
myStr[2] = 'x';
myStr[3] = '\0';
*myNum = 9;
}
int main (void) {
char someString[4];
int n = 6;
AFunc(someString, &n);
printf("%s %d", someString, n);
return 0;
}
which outputs:
pax 9
A "string" in C is really an array of characters terminated by the \0 (NUL) character.
What the above code does is to pass in the address of the first character in that array and the function populates the four characters starting from there.
In C, a pointer to char isn't necessarily a string. In other words, just because you have char *x;, it doesn't mean that x is a string.
To be a string, x must point to a suitably allocated region which has a 0 in it somewhere. The data from the first character that x points to and up to the 0 is a string. Here are some examples of strings in C:
char x[5] = {0}; /* string of length 0 */
char x[] = "hello"; /* string of length 5, the array length being 6 */
char *x = "hello"; /* string of length 5. x is a pointer to a read-only buffer of 6 chars */
char *x = malloc(10);
if (x != NULL) {
strcpy(x, "hello"); /* x is now a string of length 5. x points
to 10 chars of useful memory */
}
The following are not strings:
char x[5] = "hello"; /* no terminating 0 */
char y = 1;
char *x = &y; /* no terminating 0 */
So now in your code, AFunc's first parameter, even though is a char * isn't necessarily a string. In fact, in your example, it isn't, since it only points to a memory that has one useful element, and that's not zero.
Depending upon how you want to change the string, and how the string was created, there are several options.
For example, if the myStr points to a writable memory, you could do something like this:
/* modify the data pointed to by 'data' of length 'len' */
void modify_in_place(char *data, size_t len)
{
size_t i;
for (i=0; i < len; ++i)
data[i] = 42 + i;
}
Another slightly different way would be for the function to modify data until it sees the terminating 0:
void modify_in_place2(char *data)
{
size_t i;
for (i=0; data[i]; ++i)
data[i] = 42 + i;
}
You are only dealing with chars and char pointers. None of the char pointers are valid strings as they are not null terminated.
Try defining a string and see what it looks like.
But what if the string was more than one char? How would the code look
differently? Thanks for any help
Ofcourse, you would modify the other characters as well, but in the exact same way you did the first time.
Declare a char array and pass its address
Modify values at those address
A char array would be a more clear term for a string.