You scored 1 out of 3. Fail!
You scored 1 out of 3. Fail!
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
one the first line (function deceleration) you declared the argument as int in main you declared the array as int (int xs[].....) so one can assume unless they aren't part of main that xs[0] or xs or &xs are of the same type surely
coop
Ah, perhaps we have found a serious misunderstanding. In this function definition:
The type of the parameter named p is not int. It is pointer to int, i.e., int*Code:void foo(int *p) {}
In this declaration and initialisation of an array named xs:
The type of xs is array of 2 ints, i.e., int[2]. The 2 ints is because of the initialisation to an aggregate containing 2 ints.Code:int xs[] = {1, 2};
For foo(xs[0]), the type of the argument is an int. You know that foo expects an int*, so this is wrong.
For foo(xs), the type of the argument is int[2]. This can be converted to int* since when passed as an argument, an array is converted to a pointer to its first element, so this is correct.
For foo(&xs), the type of the argument is pointer to the array of 2 ints itself, i.e., int(*)[2]. Hence, this is wrong, even though the address would coincide with the address of the first element of xs.
Function pointers aside, it is never ever the case that xs[0], xs, and &xs have the same type.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok so if i had an arrayand i wanted my function to only change the 3rd element i would have to pass the entire array plus another argument to the element i wanted it to change?Code:int array[5] = {1,2,3,4,5}
No. Pass a pointer to the 3rd element.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ok im not getting this
i thought you just said you couldn't do that
Where did I say you couldn't do that?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i think i get it now
i re-read your first comment about xs[0] being wrong.... i keep confusing arrays becoming pointers and indexing pointers to arraysCode:int array[5] = {1,2,3,4,5}, int *p_array = array; // p_array points at element [0] ie 1 p_array + 2;// p_array now points at element [2] ie the 3 foo(p_array)
Last edited by cooper1200; 05-13-2019 at 03:48 AM.
Aye, so foo(xs[0]) is wrong, but foo(&xs[0]) is correct and equivalent to foo(xs). For passing a pointer to the 3rd element, you could write foo(&array[2]), or more simply foo(array + 2).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
while we are on the subject of pointers in general which is correct int* my array or int *myarray i did read a thread on here somewhere that argued the case for both
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thats the page i read. the reason i am learning c is to get a good grounding then i want to learn c++ (classes etc) then get into embedded systems and the like is learning int *array going to trip me up
Nope, it's just subjective style.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i got it to compile with no warnings or errors but it still doesn't do what i want it to
Code:void get_player_names(int num_of_players, Hand *p_player) { int i, x, str_index = 0; char buff[2], str_temp[10];// *p_str_temp = str_temp;// *p_buff = buff; for (i = 0; i < num_of_players; i++) { printf("Please enter player %d's name: ", i + 1); x = 0; str_index = 0; while (x != 1) { fgets(buff, sizeof(buff), stdin); if (!strcmp(buff, "\n")) { strcpy(&str_temp[str_index], &buff[0]); str_index++; } else { strcpy(&str_temp[str_index], &buff[1]);// add the /0 onto the end of str_temp flush_line();//get rid of the "\n" x = sscanf(str_temp, "%s", p_player[i].player_name); if (x != 1) { printf("Incorrect Entry\n Please enter player %d's name: ", i + 1); x = 0; } } } } for (i = 0; i < num_of_players; i++) { printf("Player %d's name is %s\n", i, p_player[i].player_name); } }
lets say the first players name is tom and the seconds is josie i want it to assign "tom" then move onto the next one without waiting for more input and therefore overwriting tom