Mysterious C function calling - c

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.

Related

Difference of char **a and char *a[]

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

Array of Strings to functions and use strcpy

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.

what is the difference between these two function pointer declarations?

int *(*const fun[])(int argc, char **argv)
and
const int *(* fun[])(int argc, char **argv).
Is the first one the array of const function pointers returning integer pointer ?
The first one is an array of read-only pointers (i.e. you can't change fun[i]) to a function receiving an int and a char **, and returning a pointer to int.
The second is pretty similar, except that you can change fun[i], but the function it points to returns a pointer to a read-only integer.
So, in short:
/* First declaration
int *(*const fun[])(int argc, char **argv)
*/
int arg1;
char **arg2;
int *example = (*fun[i])(arg1, arg2);
*example = 14; /* OK */
example = &arg1; /* OK */
fun[i] = anoter_function; /* INVALID - fun[] is an array of read-only pointers */
/* Second declaration
const int *(* fun[])(int argc, char **argv)
*/
const int *example2 = (*fun[i])(arg1, arg2);
fun[i] = another_function; /* OK */
*example2 = 14; /* INVALID - fun[i] returns pointer to read-only value. */
example2 = &arg1; /* OK */

Swap char with table pointers in C

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);

How to put values to const char **?

If I declare a variable const char ** stringTable, how should I be able to put values to it if it is a const? (It has to be const because a function I am supposed to use takes const char ** as a parameter.)
Edit:
No you cannot convert from char ** to const char ** implicitly. Compiler complains:
cannot convert parameter 3 from 'char **' to 'const char **'
Wow, I'm surprised nobody got this! Maybe I can get a necromancer badge. Implicit const casting only scans one level deep. So a char* can become a const char* but it won't dig deep enough inside the a char** type to find what needs to be changed to make it a const char**.
#include <iostream>
using namespace std;
void print3( const char **three ) {
for ( int x = 0; x < 3; ++ x ) {
cerr << three[x];
}
}
int main() {
// "three" holds pointers to chars that can't be changed
const char **three = (const char**) malloc( sizeof( char** ) * 3 );
char a[5], b[5], c[5]; // strings on the stack can be changed
strcpy( a, "abc" ); // copy const string into non-const string
strcpy( b, "def" );
strcpy( c, "efg" );
three[0] = a; // ok: we won't change a through three
three[1] = b; // and the (char*) to (const char*) conversion
three[2] = c; // is just one level deep
print3( three ); // print3 gets the type it wants
cerr << endl;
return 0;
}
Apart from other mentions that you can pass char** into function that takes const char **,
const char** is a non-const pointer to const char*, you can declare it and freely put values of type const char* in it.
On the other hand, you would not be able to do it, if you declared it as const char * const * or const char * const * const.
yourfunc(const char **p);
...
const char *array_str[10];
array_str[0] = "foo"; /* OK, literal is a const char[] */
yourfunc(array_str);
Here is what cdecl says:
cdecl> explain const char **table
declare table as pointer to pointer to const char
cdecl> explain const char * const *table
declare table as pointer to const pointer to const char
cdecl> explain const char * const * const table
declare table as const pointer to const pointer to const char
That const declaration is a quarantee of the function, you dont have to fullfill it. That means the function will keep your array untouched (it will just read). So you can pass a nonconst variable to a function expecting const.
You can pass a char ** to a function declared as taking a const char ** -- Might be worth taking a look at the documentation for const on MSDN
char ** can be converted to const char **, so if you want to call a function which takes a const char ** as a parameter, just supply your char ** and it'll be implicitly converted.
If you want to write a function which takes a const char ** as parameter and then modifies the char data it references, you're breaking the contract with your compiler, even if you might get it to work via casts!
With my compiler (gcc version 3.4.4 in cygwin), I found that I could pass char * to const char *, but not char ** to const char **, unlike what most of the answers are saying.
Here is one way you can build something up that works; maybe it will help you.
void printstring( const char **s ) {
printf( "%s\n", *s );
}
int main( int argc, char** argv ) {
char *x = "foo"; // here you have a regular mutable string
const char *x2 = x; // you can convert that to a constant string
const char **y = &x2; // you can assign the address of the const char *
printstring(y);
}
you can un-const char* by using a cast operator: (char*)
void do_something(const char* s)
{
char* p=(char*)s;
p[0]='A';
}
use the same idea with the arrays char**
const char ** indicates that the underlying character is constant. So, while you can't do something like this:
const char **foo = ...;
**foo = 'a'; // not legal
but there is nothing preventing you from manipulating the pointer itself:
// all the following is legal
const char **foo = 0;
foo = (const char **)calloc(10, sizeof(const char *));
foo[0] = strdup("foo");
foo[1] = strdup("baz");
That said, if you did want to modify the actual character data, you could use a non-const pointer and cast it:
char **foo = ...;
func((const char **)foo);

Resources