Passing array from main and modify it through another function - c

So my question is that if I have the following code
main(){
char arr[1][3];
foo(arr);
}
void foo(char arr1[1][3]){
arr1[0] = "AB\0";
}
Does this mean that the value in arr from main would also be modified into "AB\0"?

Try checking this stack overflow question and answer, it is an age-old question
Passing an array by reference in C?

Why not test it out yourself?
#include <stdio.h>
void foo(char arr[]);
int main(int argc, char **argv)
{
char arr[5] = "test";
printf("%s\n", arr);
foo(arr);
printf("%s\n", arr);
return 0;
}
void foo(char arr[])
{
arr[0] = 'p';
}

Related

How to call a function with an array of strings

I've made a function, but I'm struggling with calling it.
This is the prototype of the function:
char *test(int argc, char **argv);
I've tried calling it this way but it doesn't work :
int main()
{
char tab[3][3] ={
"Yo",
"Hi"};
test(2, tab);
return (0);
}
For me this works:
char* test(int index, char** char2Darray)
{
return char2Darray[index];
}
int main()
{
char* tab[2] ={
"Yo",
"Hi"};
test(1, tab);
return (0);
}
I think there are two problems in your code :
the tab what you provided has only two item
your tab is a pointer which pointing to char[3] types and not char*
When you pass the 2D array
char tab[3][3]
to the function test(), it decays to a pointer of type:
char (*)[3]
which is a pointer to an array of 3 char elements.
This type is not compatible with char** which is a pointer-to-pointer to char. This is the source of your problem. You need to change the function test() to take char (*argv)[3] instead of char **argv.
Looking at function prototype:
char *test(int argc, char **argv);
char **argv is pointer of pointers and the variable char tab[3][3] is array of arrays so they are incompatible.
You can change function prototype to: char *test(int argc, char argv[][SOMECONSTANT]); or char *test(int argc, char (*argv)[3]);
This way
char * test(int argc,const char **argv);
int main()
{
const char * tab[3]=
{
"Yo",
"Hi",
0
};
test(2,tab);
return 0;
}
or
char * test(int argc,char **argv);
int main()
{
char arg1[]="Yo";
char arg2[]="Hi";
char * tab[3]=
{
arg1,
arg2,
0
};
test(2,tab);
return 0;
}

C pointer to string returning warning [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following code:
int ver(unsigned char** v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
I get the following warning that the pointer differs in signedness. Can you please advise.
Change the declaration of ver to:
int ver(unsigned char * v)
I.e. get rid of one of the * characters.
Warning about signedness is because strcpy and printf("%s") expect a char*, but you are passing unsigned char*
Also int ver(unsigned char ** v) should be int ver(char* v)
Actually the problem is this :
int ver(unsigned char ** v)
You have to change the function to that :
int ver(unsigned char * v)
You need to do 2 fixes:
1) Follow what others told you about the method declaration, changing to "int ver(unsigned char* v) {" ;
2) Just include <string.h> at your code.
Example:
#include <stdio.h>
#include <string.h>
int ver(unsigned char* v) {
unsigned char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char* argv[]) {
unsigned char s[10];
ver(s);
printf("version = %s", s);
return 0;
}
The cause of the warning:
strcpy() expects a signed char*, and is called with a unsigned char*.
But there should be other warnings as well:
Function ver() expects a pointer to a pointer:
int ver(unsigned char ** v){
ver(s) sends a single pointer to char:
unsigned char s[10];
ver(s);
To solve, you can change the code to:
int ver(char *v) {
char str1[] = "1.0.2";
strcpy(v, str1);
return 0;
}
int main(int argc, char *argv[]){
char s[10];
ver(s);
printf("version = %s", s);
return 0;
}

c , passing char array as parameter in function?

int check(int i,int j,char test);
int main(int argc, char *argv[])
{
char mat[5][5];
char *anahtar;
anahtar=(char*)malloc (length*sizeof(char));
//i take length from user with scanf
int k=0;
if (check(i,j,anahtar[k])==1)
{
mat[i][j]=anahtar[k];
}
int check(int i,int j,char test)
{
int a=0;
int b=0;
if (mat[a][b]==test)
{
return 1;
}
else
{
return 0;
}
}
}
It gives error
undefined reference to `check'|
anahtar[] is a char array.So why cant i pass anahtar[k] in argument?
I already have protoype. PRoblem is not that.
The problem is that your check function is inside the main. Place it outside of main.
Also do not cast malloc;
anahtar = malloc (length*sizeof(char));
You need to declare it before you use it or
put this beforehand
int check(int i,int j,char test);

Memset not working in outside function

This is giving me a segfault at the memset and I have no idea why, I am going to a specific index of a 2D array, this should give me a char pointer and allow me to use memeset.
void test(char** test)
{
int i;
for(i=0;i<20;i++)
{
memset(test[i],0,sizeof(char)*1);
return;
}
}
int main()
{
char thing[20][20];
int i;
for(i=0;i<20;i++)
{
memset(thing[i],0,sizeof(char)*20);
}
test(thing);
return 0;
}
Your parameter declaration is incorrect, it should be:
void test(char test[20][20])
or:
void test(char test[][20])

Reference to a pointer is lost [duplicate]

This question already has answers here:
Assignment of function parameter has no effect outside the function
(2 answers)
Closed 9 years ago.
I am trying to understand why this statement doesn't work.
char resp[] = "123456789";
void getValue(char *im)
{
im = resp;
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
Output:
123456789
IMEI:
You can not assign with =, use strcpy instead:
#include <stdio.h>
#include <string.h>
char resp[] = "123456789";
void getValue(char *im)
{
im = strcpy(im, resp);
printf("\n%s\n",im);
}
int main(int argc, char *argv[])
{
char imei[11] = {0};
getValue(imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
That's because imei is an array[11] (not just a pointer to), if you want to assign via = you can:
#include <stdio.h>
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
int main(int argc, char *argv[])
{
char *imei; /* Not an array but a pointer */
getValue(&imei);
printf("\nIMEI: %s\n",imei);
return 0;
}
C passes parameters by value. Whatever change you ale to the im will be lost when the function exits. If you want to preserve the change. Pass the address of the pointer. Then you can change the pointer at the address you pass.
Try this:
char resp[] = "123456789";
void getValue(char **im)
{
*im = resp;
printf("\n%s\n",*im);
}
You need to pass a pointer to a pointer as your program argument.

Resources