I have an array of n strings where n is not known at compilation time.
The real input is a giant string that I will splice, and add the parts to each position of the array.
In the example I've simulated a sentence with n=3 , but n can be any number.
void addWords(char *array[][300], int n) {
char p[] = "Hello ";
char p1[] = "World ";
char p2[] = "!";
strcpy(array[0],p);
strcpy(array[1],p1);
strcpy(array[2],p2);
printf("%s%s%s\n",array[0],array[1],array[2]);
}
int main(int argc, char const *argv[])
{
int n = 3;
char array[n][300];
addWords(array,3);
return 0;
}
The code gives segmentation fault and I cannot identify the cause.
//void addWords(char *array[][300], int n) {
void addWords(char array[][300], int n) { // <== use `char array[][300]`
char p[] = "Hello ";
char p1[] = "World ";
char p2[] = "!";
strcpy(array[0],p);
strcpy(array[1],p1);
strcpy(array[2],p2);
//printf("%s%s%s\n",p[0],p[1],p[2]);
printf("%s%s%s\n",array[0],array[1],array[2]); // <== I think you meant `array` instead of `p`
}
This
void addWords(char *array[][300],
^^^^^^^^^^^^^^^^^^
means
pass a pointer to an array containing 300 char pointers
What you want to say is
pass a pointer to an array containing 300 char
So all you need is:
void addWords(char *array[][300], --> void addWords(char array[][300],
I see that you declared 'addWords' with argument of type "char *array[][300]", but pass value of type "char array[n][300]". Different types.
Related
I am new to programming and C in general and the last few weeks I try to get the concept of pointers, arrays and how they are connected.
At the moment I experiment with command line arguments in C and I read here on this platform that argv can be syntactically defined differently, however the semantic stays the same.
int main(int argc, char *argv[])
is equal to
int main(int argc, char **argv)
Okay, but why is my code behaving differently when I try to initialize an array in these ways:
char *s[] = {"hallo", "12345"};
printf("%c und %c", s[0][4], s[1][2]);
I get as output as expected: o and 3.
But when I initialize the array like
char **s = {"hallo", "12345"};
printf("%c und %c", s[0][4], s[1][2]);
I get a segmentation fault or other errors which I cannot understand like (near initialization for āsā)
I guess you cannot initialize an pointer to a pointer array with 2 asterisks.
Maybe someone can provide me with more information about these relation and how these 2 definitions differ from each other.
They are completely different:
char *s[]
is an array of pointers. Arrays cannot be assigned or used as values
This code will not compile:
char *a[] = {"hallo", "12345"};
char *a1[2];
char **s = (char *[]){"hallo", "12345"};
char **s1;
void foo(void)
{
a++;
}
void bar(void)
{
a1 = a;
}
char **s
is a pointer to a pointer and can be assigned or used as an lvalue.
This code will compile:
char *a[] = {"hallo", "12345"};
char *a1[2];
char **s = (char *[]){"hallo", "12345"};
char **s1;
void foo(void)
{
s++;
}
void bar(void)
{
s1 = s;
}
https://godbolt.org/z/vqxv913WY
I have been trying to solve this issue for whole day, and could not do it on my own. Searching the internet didn't help me solve it either
So, this the function prototype:
void invert(char **arr, int n);
First argument is an array of strings, and the second one is number of strings in an array.
This is my code:
#include <stdio.h>
#include <string.h>
void invert(char** arr, int n)
{
int i, j, len;
for(j=0;j<n;j++)
{
len=strlen(arr[j]);
for(i=0;i<len/2;i++)
{
char tmp = arr[j][i];
arr[j][i] = arr[j][len - i - 1];
arr[j][len - i - 1] = tmp;
}
}
}
int main()
{
int n=3, i;
char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";
invert(arr, n);
for(i=0;i<3;i++)
{
printf("%s ",arr[i]);
}
}
The code breaks when it reaches the line:
arr[j][i] = arr[j][len - i - 1];
and I can't figure out why.
The function receives an array of strings perfectly (tested it with some printf statements for characters of specific strings), and the char tmp succesfully recieves a correct character, but the program crashed when it reaches the line mentioned earlier. Printf statements after that line don't work.
Did I miss anything? Can someone explain what am I doing wrong? Thank you!
For starters this code snippet
char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";
invokes undefined behavior because the pointer arr is uninitialized and has an indeterminate value.
Moreover this approach in any case is wrong because you may not change string literals.
What you need is to declare a two-dimensional array as for example
enum { N = 11 };
//...
char arr[3][N] =
{
"John", "Doe", "Programmer"
};
In this case the function declaration will look like
void invert( char arr[][N], int n );
The enumeration must be declared before the function declaration.
Instead of the two-dimensional array you could declare an array of pointers like
char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };
In this case the function declaration may be as shown in your question
void invert(char** arr, int n)
So what you need to do with minimal changes is to substitute this code snippet
char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";
for this code snippet
char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };
To begin with, what you have here:
char **arr;
is a pointer to pointer to char.
Secondly, even if you had an array of pointers to char, like so :
char *arr[3];
And then assigning each string literal :
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";
would still invoke Undefined behavior, since you are attempting to modify a string literal which is read only.
What you need is, either a 2D array of chars :
char arr[][100] = {"John", "Doe", "Programmer"};
and also change the function signature to :
void invert(char arr[][100], int n)
or you have to dynamically allocate memory and use a function like strcpy(), strdup(), memcpy() etc :
char **arr;
arr = malloc(n * sizeof(char *)); // or sizeof(*arr)
if (arr == NULL) {
fprintf(stderr, "Malloc failed to allocate memory\n");
exit(1);
}
arr[0] = strdup("John"); // good idea to also check if strdup returned null
arr[1] = strdup("Doe");
arr[2] = strdup("Programmer");
invert(arr, n);
for(i=0;i<3;i++)
{
printf("%s ",arr[i]);
}
for (i = 0; i < 3; i++) {
free(arr[i]);
}
free(arr);
So I am quite new to using pointers and wanted to know how I could do the following but for a string value.
int number(int, int *);
int main()
{
int a = 15;
int b =0;
number(a,&b);
fprintf(stdout,"Number b is %d\n",b);
return 0;
}
int number(int a, int *b) {
*b = a;
}
It seems you mean something like the following
#include <stdio.h>
char * assign( char **s1, char *s2 )
{
return *s1 = s2;
}
int main(void)
{
char *s1 = "Hello World!";
char *s2;
puts( assign( &s2, s1 ) );
return 0;
}
The program output is
Hello World!
That is to assign a value to the pointer s2 (that has the type char *) within the function you need to pass it to the function by reference as you are doing in your program with an object of the type int. Otherwise the function will deal with a copy of its argument and changes the copy will not influence on the argument.
Passing an object by reference in C means passing an object indirectly through a pointer to it.
If you have character arrays that store strings then to copy a string to a character array you can use the standard string function strcpy declared in the header <string.h>.
I'm trying to swap two char with two table pointers.
Can someone explain to me what's wrong in my code?
The terminal says char** is expected but I don't know what to do, so I think I don't really understand how pointers work for tables.
void echangeM2(char **ptab1, char **ptab2){
char *tmp = *ptab1;
*ptab1 = *ptab2;
*ptab2 = *tmp;
printf("%s\t %s",*ptab1,*ptab2);
return;
}
int main(void) {
char tab1[25];
char tab2[25];
char *adtab1;
char *adtab2;
*adtab1 = &tab1;
*adtab2=&tab2;
printf("type two words");
scanf("%s %s",tab1,tab2);
echangeM2(adtab1,adtab2);
return 0;
}
The following code should work for you:
#include <stdio.h>
void exchangeM2(char* *ptab1, char* *ptab2) { // accepts pointer to char*
char* tmp = *ptab1; // ptab1's "pointed to" is assigned to tmp
*ptab1 = *ptab2; // move ptab2's "pointed to" to ptab1
*ptab2 = tmp; // now move tmp to ptab2
printf("%s\t %s",*ptab1,*ptab2);
}
int main(void) {
char tab1[25];
char tab2[25];
char* adtab1;
char* adtab2;
adtab1 = tab1; // array name itself can be used as pointer
adtab2 = tab2;
printf("type two words");
scanf("%s %s",tab1,tab2);
exchangeM2(&adtab1, &adtab2); // pass the address of the pointers to the function
}
echangeM2(&adtab1,&adtab2);
This should fix the compile errors. You are passing char* pointers to a function that expects a char ** pointer
Edit: Actually looks like you want something like
char **adtab1;
char **adtab2;
adtab1 = &tab1;
adtab2=&tab2;
...
echangeM2(adtab1,adtab2);
I have this code:
int indexOf(const char *array[], char *e)
{
printf("inside: %d\n",(int)sizeof(array));
/* ... */
}
int main(int argc, char *argv[])
{
const char *a[] = {";", ",", ":", "==", ":="};
char *b = "==";
printf("outside: %d\n",(int)sizeof(a));
int d = indexOf(a,b);
/* ... */
}
And this is the output:
outside: 40
inside: 8
Why output is not the same? Any help, please?
The array decays into a pointer to it's first element when passed to a function. The sizeof from the function yields the size of the pointer on your implementation. You could have declared it:
int indexOf(const char **array, char *e)
You will probably want to pass the length as a separate parameter.
EDIT
What I'm trying is to avoid to pass the size as a parameter.
In that case you could mark the end of the array with a NULL
const char *a[] = {";", ",", ":", "==", ":=", NULL};
That way in the function you will know where it ends.