I learned pointers and arrays already but I don't know what ** or && is used for and when trying to understand this line im not sure what this is.
int main() {
srand(time(NULL));
int n, m, option, row, col, flagCount = 0;
char **flags, **board;
im not sure what's being declared in the 4th row also those pointers at list I think they are are later in the program being sent to this function .
char **creatArray(int n)
what is this (**) used for in the function type?
* is a pointer type, ** is a pointer type to a pointer, for example: int* p;, meaning that p is a pointer type that points to int, and if you need a pointer type to this p pointer, you can do the following definition: int** test = &p; At this point test is a pointer to p, its type is int**.
char *flag;
it means that flag is a pointer which will hold the address of character data type
char **flag;
it means that flag is a pointer which will hold the address of a "POINTER" which is pointer to a character data type
enter image description here
When you use a pointer '*' you can create for example an array when you allocate memory for multiple chars. Example char *word = malloc(10*sizeof(char)). Your word has now space for 10 chars. When you want to create a sentence you need multiple words so you can create a two dimensional array. You can say char *sentence = malloc(10*sizeof(word)).
In c double pointer are often used to create a two dimensional array.
When your function create a new game board of size n*n then it will return the array.
So for example you call char **board = **newArray(n);
In c you can create an array like char array[] or you can create an like char *array. This is the same because an array in C is a pointer to the first element of the array. But the problem is you can't return an array like this from a function you need to return the pointer. So char[] function() is not working you need to say char *function(). And this is the case in your example you create a new two dimensaional array for your board and return the pointer to the first element (pointer) of the array that is why you call your function char** and not char[][].
Related
I have a two dimensional array like this:
void getC(int **p)
{
*p = &c[0][0];
}
int c[10][10];
int *a;
getC(a);
a[0][0];
it says error: no match for 'operator[]' in `a[0][0];` what is the problem and how to fix it?
You're compiling a C program with a C++ compiler. Watch out!
You need to put the definition of c above the getC function (or provide a forward declaration).
You have statements outside of a function, which isn't allowed in C. Wrap the int *a and subsequent lines with int main(void) { ... }
You need a & to make your getC() call legal - you're passing an int *, but it expects int **:
getC(&a);
The statement a[0][0] has no effect, and is anyway wrong since a is just an int *; you can't dereference it twice.
You should probably get a beginner C book and start working through it.
Essentially you are sort of downgrading the array/pointer from an int (*)[10] (pointer to array of 10 int) to a simple int pointer, by just returning the address of the first element of the 2dim array. While this is techically correct (the address of one element of the 2dim array is of course an int*), the information about the structure/layout of the ints in the array is lost, so the resulting a-ptr doesn't now anything about the fact that the int was part of a [10][10] structure.
In your case, the only way to get to the array elements would be to multiply your way through the int arrays, based on your own knowledge that at address a there are 100 ints organized 10x10:
int *a;
getC(&a);
...= a[10*x + y]; // equivalent of c[x][y];
.
However, essentially, the correct way (completely preserving types) would be
int c[10][10];
void getC(int (**p)[10]) // pointer to pointer to array of 10 ints
{
*p = c; // c itself can seamlessly change into a pointer to one sub-element
// (i.e. pointer to array of 10)
}
int main()
{
int (*a)[10]; // pointer to array(s) of 10 ints
int q;
getC(&a);
q= a[9][9];
...
}
The same again with one more dimension level (probably the most intutive solution):
However, essentially, the correct way (completely preserving types) would be
int c[10][10];
void getC(int (**p)[10][10]) // pointer to pointer to array of 10x10 ints
{
*p = &c; // &c can seamlessly change into a pointer to 10x10 ints
}
int main()
{
int (*a)[10][10]; // pointer to array(s) of 10x10 ints
int q;
getC(&a); // pass adress of pointer to 10x10 ints
q= (*a)[9][9]; // need *a in brackets to derference the pointer (operator precedence)
...
}
Basically, I have an array of char* that I want to pass and modify in this function, so I pass in a pointer to an array of char*. That is, I want to pass a pointer to char* arr[]. What is the difference between the two?
As always, http://cdecl.org is your friend:
char * (*arr)[] - "declare arr as pointer to array of pointer to char"
char *** arr - "declare arr as pointer to pointer to pointer to char"
These are not the same. For a start, the first is an incomplete type (in order to use a pointer to an array, the compiler needs to know the array size).
Your aim isn't entirely clear. I'm guessing that really all you want to do is modify the underlying data in your array of char *. If so, then you can just pass a pointer to the first element:
void my_func(char **pointers) {
pointers[3] = NULL; // Modify an element in the array
}
char *array_of_pointers[10];
// The following two lines are equivalent
my_func(&array_of_pointers[0]);
my_func(array_of_pointers);
If you really want to pass a pointer to an array, then something like this would work:
void my_func(char *(*ptr)[10]) {
(*ptr)[3] = NULL; // Modify an element in the array
}
char *array_of_pointers[10];
// Note how this is different to either of the calls in the first example
my_func(&array_of_pointers);
For more info on the important difference between arrays and pointers, see the dedicated chapter of the C FAQ: http://c-faq.com/aryptr/index.html.
If you have a function that has char *(*arr)[] as a parameter, you will need to pass in an array with the address operator:
void afunc(char *(*arr)[]);
char *charptra, *charptrb, *charptrc;
char *arr[] = {charptra, charptrb, charptrc};
afunc(&arr);
On the other one, you have to pass a pointer that points to a pointer that points to a pointer:
void afunc(char ***);
char arr[] = "str";
char *arrptr = arr;
char **arrptrptr = &arrptr;
char ***arrptrptrptr = &arrptrptr;
afunc(arrptrptrptr);
I learning to use Pointers.
I have a few questions about an exercise code I wrote.
First, if I have the following function code:
//function prototype
void processCars(char *, char []);
int main(){
...
//actual callto the function
processCars(strModel[x], answer);
...
}
void processCars(char * string1, char string2[])
{
...
}
How would it be correct the define the arguments of this processCars function?
The first one - char * - is a pointer to char, which is the start location of the string (or better an array of chars) ?
The second is actually an array of chars directly.
Now, supposed I want to pass by reference a few array of strings and even array of structs. I managed to create the following code which works, but I still don't fully get what I'm doing.
typedef struct car {
char make[10];
char model[10];
int year;
int miles;
} aCar; // end type
// function prototype
void processCars( aCar * , char **, char **, int *, int *);
//aCar * - a pointer to struct of type car
//char **, char **
// int * - a pointer to integer
// Arrays passed as arguments are passed by reference.
// so this prototype works to
//void processCars( aCar * , char **, char **, int [], int []);
int main(){
aCar myCars[3]; // an array of 3 cars
char *strMakes[3]={"VW","Porsche","Audi"}; // array of 3 pointers?
char *strModel[3]={"Golf GTI","Carrera","TT"};
int intYears[3]={2009,2008,2010};
int intMilage[3]={8889,24367,5982};
// processCars(myCars, strMakes);
processCars(myCars, strMakes, strModel, intYears, intMilage);
return 0;
} // end main
// char ** strMakes is a pointer to array of pointers ?
void processCars( aCar * myCars, char ** strMakes, \
char ** strModel, int * intYears, \
int * intMilage ){
}
So, my qeustion is how to define this "char ** strMakes". What is it, why is it working for me?
I have also noticed, I can't change part of the string, because if I correct (or the references I read)
strings are read only. So, like in python, I can use array indices to access the letter V:
printf("\n%c",strMakes[0][0]);
But, unlike in python, I can't change it:
strMakes[0][0]="G" // will not work? or is there a way I could not think of?
So, thanks for reading through my long post and many question about pointers and c strings. Your answers are appreciated.
Within the function itself, both arguments will be pointers. The [] in the parameter list makes absolutely no difference, it's just syntactic sugar.
Although there is a distinct difference between an array and a pointer, a passed array to a function always decays to a pointer of the corresponding type. For example, an array of type char[3] would decay to char *, char *[3] would decay to char **.
char *strMakes[3] is an array of length 3 that holds pointers to strings stored somewhere else, probably in a read-only area of the process. An attempt to modify the strings themselves will result in undefined behaviour, most probably a protection fault.
If you want to be able to modify the strings, you could declare it as an array holding arrays, not pointers:
char strMakes[3][20] = {"VW", "Porsche", "Audi"};
This way the strings will be stored consecutively within the bounds of the outer array.
Another way is to still have an array of pointers, but the pointers should point to mutable memory:
/* these strings are mutable as long as you don't write past their end */
char str1[] = "VW";
char str2[] = "Porsche";
char str3[] = "Audi";
char *strMakes[3] = {str1, str2, str3};
My program is
#define ARRLEN 10
#define SIZEALCO 128
#define MAX_STRING_LENGTH 12
in main function,
char TYPEDATA_XML_FN[ARRLEN][SIZEALCO];
char TYPEDATA_MD5_FN[ARRLEN][SIZEALCO];
char identifier[ARRLEN][MAX_STRING_LENGTH];
char Temppath[SIZEALCO];
int arraynum;
// ...
arraynum = 0;
for(arraynum = 0; arraynum <ARRLEN; arraynum++)
{
/* Create the file name with the path*/
strcpy(Temppath,"/fw/TYPEDATA/");
nameFil(Temppath,identifier[arraynum],TYPEDATA_XML_FN[arraynum],TYPEDATA_MD5_FN[arraynum]);
}
subfunction is :
void nameFil(char *SourPath,char *InPinName,char *FilePathNameXml,char *FilePathNameMd5)
{
sprintf(FilePathNameXml, "%s\\%s_TYPEDATA.XML",SourPath,InPinName);
sprintf(FilePathNameMd5, "%s\\%s_TYPEDATA.MD5",SourPath,InPinName);
}
I checked with your example. I used (trial)
char** a = calloc(ARRLEN, sizeof(char *));
for(i = 0; i < ARRLEN ; ++i)
a[i] = ucmalloc(MAX_STRING_LENGTH);
pase(a);
subfunction :
void pase(char b[ARRLEN][MAX_STRING_LENGTH])
{
// ...
}
Now I got the warning message as "warning: passing arg 1 of `pase' from incompatible pointer type".
Actually, I would like to pass the full string array identifier,TYPEDATA_XML_FN,TYPEDATA_MD5_FN. Now I am passing single string to the subfunction. Kindly guide me. Thank you
The prototype void pase(char b[ARRLEN][MAX_STRING_LENGTH]) is rather mis-leading,
void pase(char b[][MAX_STRING_LENGTH])
would be better, since otherwise there is the implication of bounds checking (the first array dimension is ignored).
The reason why you get "incompatible pointer type" is because a is an array of pointers. If a was incremented (as a pointer itself) then the address would increase by the size of a pointer. However, b is an array of arrays of size MAX_STRING_LENGTH, so if b was incremented then the value would increase by MAX_STRING_LENGTH.
The way you have allocated the array a will (probably) not give you contiguous memory, which is what is required here. You could achieve what you want using an array of pointers, but you really must decide what you want to do. If you want to use [][] notation then you need to
calloc(MAX_STRING_LENGTH,ARRLEN);
You are getting confused because although an one dimensional array char[] behaves like a pointer char *, in two dimensions a char[][N] is not convertible to a char **, being actually more like a (*char)[N] (pointer to arrays of length n of char).
So if you want to make a function that receives a two dimensional array, you have two choices:
Use pointers to pointers:
void f(char ** array, int nrows, int ncols);
To create a char**, do like you are already doing now: create an array for pointers and call malloc for each one of them.
Use two dimensional arrays:
void f(char array[][NCOLS], int nrows);
//note: NCOLS is a compile time constant now
//NROWS is the first dimension and can be omited from array[NROWS][NCOLS]
The tricky bit is malloc-ing a two dimensional array:
char (*my_array)[NCOLS];
//my_identifiers is a pointer to arrays of length NCOLS
// it can be passed to any function expecting a car[][NCOLS]
my_array = malloc(number_of_rows*(sizeof *my_array));
You can also make it easier to understand all of this with a good choice of typedefs:
typedef char MY_STRING[MAX_STR_LENGTH];
//define my strings as arrays of MAX_STRING_LENGTH
void f(MY_STRING array[]);
...
MY_STRING *arr = malloc(nstrings*sizeof(MY_STRING));
f(arr);
#define STRMAX 50
struct Person {
char sName[STRMAX];
int iAge;
};
typedef struct Person PERSON;
int main() {
PERSON *personen[1];
personen[0]->sName = "Pieter";
personen[0]->iAge = 18;
return 0;
}
This code generates an error on personen[0]->sName = "Pieter"; saying incompatible types in assignment. Why?
You don't want an array of pointers. Try
PERSON personen[1];
And like others have said, use the strcpy function!
Don't try to assign arrays. Use strcpy to copy the string from one array to the other.
...sName is an array of chars while "Pieter" is a const char*. You cannot assign the latter to the former. The compiler is always right :)
Change
PERSON *personen[1];
to
PERSON personen[1];
and use strcpy to copy the string.
strcpy(personen[0]->sName,"Pieter");
I agree with the above but I figured it was also important to include the "why"
int a; // is an integer
int *b; // pointer to an integer must be malloced (to have an array)
int c[]; // pointer to an integer must also be malloced (to have an array)
int d[5]; // pointer to an integer bu now it is initialized to an array of integers
to get b and c from simple pointers and give them memory to match d use the following to give them memory space
b = (int *) malloc(sizeof(int)*5);
where it casts the pointer returned from malloc to an int pointer, and creates a memory block of 5 times the size of an integer (thus it will hold 5 integers like d)