C - Passing array between functions not giving expected results - c

I'm looking to try and pass a char array from one function to another. The array seems to pass into the second function, but the contents of the array seem to be lost, apart from the first element? To get round this I tried duplicating the content of the passed array into a new local array in the called function, but this then opened up the issue of passing the new array back to the original calling function and assigning it's value back to the originally passed in array.
It's likely my pointers could be completely wrong, I'm still getting to grips with pointers in C so have been following tips from the IDE until it stops throwing errors.
Here is how I'm calling the function:
wordCheck(charIn, randWord, currentWordStatus);
The array to be updated/modified is the currrentWordStatus array.
This is the content of the array when it is passed into the function:
And here is how it actually ends up in the function:
Here is the code of my called function:
void wordCheck(char guess, char *wordToGuess, char *currentWordStatus) {
int wordLen = strlen(wordToGuess);
char wordArr[wordLen];
strcpy(&wordArr[0], wordToGuess);
bool found = false;
for (int i = 0; i < wordLen; i ++) {
printf("%c \n", (char)wordArr[i]);
if ((char) wordArr[i] == guess) {
found = true;
currentWordStatus[i] = guess;
}
}
}
I was wondering what I'm doing wrong, I'm hoping to modify the function directly as it is passed 'By Reference' (I'm aware it's a pointer to the start of the array), instead of having to remake the array inside of the function and passing it back.
Hopefully that makes some sort of sense?
Many Thanks

In the function all you really have is a pointer to the first element of the array. The program (or the compiler, and as you noticed not even the debugger) doesn't really know what a pointer is really pointing to inside a function.
There's nothing wrong, it's just how C and arrays and pointers in C works.

Related

Why are pointers such a big deal in C?

I understand the premise of pointers, but I find it very annoying, and I don't get why it's considered useful;
I've learned about pointers, and the next thing I know, I start seeing bubbles, asterisks, and ampersands everywhere.
#include <stdio.h>
int main () {
int *ptr, q;
q = 50;
ptr = &q;
printf("%d", *ptr);
return 0;
}
why is this important or useful?
First, parameters passed to a function can only be primitives(int, char, long....), structs or pointers. Then if you need to pass a more complex element like an array (strings) or a function, you have to pass a reference to this element.
The second things that I can quickly think of is: parameters are always passed by "value". This means the called function only get a copy of your variable. So, modifications will only affect the copy, the original variable will remain unchanged.
If you pass a variable by "reference" with a pointer, the pointer itself is immutable but as it is a reference to the original var, any modification to the pointed element will also affect the var in the caller function.
In other words, if you want to create a function that can alter a variable, you have to pass it a pointer to that variable to achieve this.

LeetCode: Two Sums (Error: Returning the Array)

I am new to LeetCode and am trying to improve upon my problem solving techniques. I am attempting to solve the Two Sum Problem in C and have run into trouble with the final return statement. The code initially provided to me by LeetCode was a function called "int* twoSum" and the goal is to find the two indices in an array that produce the target number. The function lists a couple parameters that I assumed were provided in main since it was not shown.
I changed the name of the function to just "int twosum" and removed the int* returnSize because I am not a big fan of unnecessary pass by address instead of by value and felt it wouldn't have a significant impact. However, after trying to run my code I run into the warning error: "returning 'int *' from a function with return type 'int' makes integer from pointer without a cast"
Could someone who understands this issue or has solved the problem before on LeetCode please provide insight as to what I need to correct? Thank you.
int twoSum(int *nums, int numsSize, int target){
int outerCount; //loop control variable for outer loop
int innerCount; //loop control variable for inner loop
int array[2]; //array that stores indices of numbers that produce target
for(outerCount = 0; outerCount < numsSize; outerCount++)
for(innerCount = outerCount + 1; innerCount < numsSize; innerCount++)
{
if(nums[outerCount] + nums[innerCount] == target)
{
array[0] = outerCount;
array[1] = innerCount;
}
}
return array;
}
The problem asks you to return two integers (indices), so a return type of int is pretty clearly incorrect. int is a single integer; two return two integers you need to return an array of int, or struct containing two integer members. C doesn't allow you to return arrays by value, so if you need to return an array, you need to return an int*. That's just the way C is.
Note that since you cannot return an array by value, you also cannot return a pointer to an automatically allocated array, since that object's lifetime will end when the function returns. So you need to dynamically allocate the array (unless it is passed to your function as an argument, which is a very common style). In this case, it is pretty clear that a dynamically allocated return value is desired, based on the comment:
/* Note: The returned array must be malloced, assume caller calls free(). */
Whether or not you like this style, you will need to conform to it for this exercise, since it is pretty clear that the caller will call free()on the returned pointer, and calling free() on a pointer not originally returned by malloc is Undefined Behaviour (and very likely to crash your program). (You can free(NULL), but that's also a violation of the calling contract, which will segfault when the caller tries to examine the non-existent return values.)
C does let you return structs by value, but if you are going to return a struct, you and the caller need to agree on its declaration (the names of its members, for example).

passing multidimensional array as argument in C

C newbie here, I need some help: Can anyone explain to (and offer a workaroud) me why this works:
int n=1024;
int32_t data[n];
void synthesize_signal(int32_t *data) {
...//do something with data}
which let me alter data in the function; but this does not?
int n=1024;
int number=1024*16;
int32_t data[n][2][number];
void synthesize_signal(int32_t *data) {
...//do something with data}
The compiler error message is something like it expected int32_t * but got int32_t (*)[2][(sizetype)(number)] instead.
First, passing arrays in C is by reference. So you pass a pointer of some sort, and the function can modify the data in the array. You don't have to worry about passing a pointer to the array. In fact, in C there is no real different between a pointer that happens to be to the being of an array, and the array itself.
In your first version. You making a one-dimensional array data[n], and you are passing it to your function. In the array, you'll using it by saying, something like data[i]. This translates directly to (data + (i sizeof(int32_t)). It is using the size of the elements in the array to find the memory location that is i positions in front of the beginning of your array.
int n=1024;
int number=1024*16;
int32_t data[n][2][number];
void synthesize_signal(int32_t *data)
In the second case, you're setting up a mufti-dimensional array (3D in your case). You setup correctly. The problem is that when you pass it to the function, the only thing that gets passed the address of the being of the array. When it gets used inside the function, you'll do something like
data[i][1][x] = 5;
Internally C is calculating how from the beginning of the array this location is. In order for it to do that, it need to know the dimensions of the array. (Unlike some newer languages, C store any extra data about array lengths or sizes or anything). You just need to change the function signature so it knows the shape/size of array to expect. Because of the way, it calculates array positions, it doesn't need the first dimension.
In this case, change your function signature to look like this:
void synthesize_signal(int32_t data[][2][number]) { ...
Setup the array the same way you are doing the second one above, and just call it you'd expect:
synthesize_signal(data);
This should fix everything for you.
The comments mention some useful information about using more descriptive variable names, and global vs. local variable. All valid comments to keep in mind. I just addressed to code problem you're having in terms of mufti-dimensional arrays.
try
synthesize_signal(int32_t** data)
{
}
Your function also needs to know that data is multi dimensional. You should also consider renaming your data array. I suspect that it is a global variable and using the same name in function can lead to problems.
When you call the function, do it like this:
synthesize_signal(&data[0][0][0]);

pointer function in C not working

I intend to make my pointer print the value, but it just stops working (no error report from the MinGW) . . .
#include<stdio.h>
void point_here(int *yolo) {
int you = 2014;
yolo = &you;
}
main () {
int *punk = NULL;
point_here(punk);
printf("Punk # %d is not dead\w/.\n", *punk);
}
How do I make this work? And why this does not work? Please explain. I'm new to pointers and C, and still confuse after reading stuff.
C is "call by value"; functions get copies of the caller's values, not references to them. If you change an argument inside the function, nothing happens in the caller's context.
To work around this you need explicit pointers, but even you still can't have a pointer to a local variable inside a function persist beyond the running of that function.
You're passing NULL as an argument to a function which effectively doesn't nothing. Then you try to dereference that NULL pointer which is illegal.
This modified version of your code works well:
#include <stdio.h>
void point_here(int *yolo) {
*yolo = 2014;
}
int main () {
int value = 0;
int *punk = &value;
point_here(punk);
printf("Punk # %d is not dead\w/.\n", *punk);
return 0;
}
As others have said, you have the following issues:
Pointer to a function value doesn't work after the function finishes executing
You aren't passing the pointer to the pointer you are passing the pointer value. (In other words, you aren't editing what you think you are).
I fixed these two issues by passing in a pointer that has already been allocated, then you just modify it in the function. This is one standard way to use pointers. Best of luck!
There are 2 problems. First the pointer variable is pass by value, second the variable "you" is on stack which gets lost once the function returns.
Change the variable you to static like
static int you = 2014;
and change the argument to **yolo;

Need help returning/passing multiple arrays

I have an assignment in my computer science class where I am writing a program that will need to return multiple array. Technically I could do this just fine, but the way the assignment is set up make it difficult for me to gauge certain things.
Basically, I have to declare two arrays and a variable in the main() function and send them to a function called input().
Input() will take these three things, which are NumberOfYears, Year[NumberOfYears],DataForYear[NumberOfYears]. NumberOfYears will be taken using a scanf(), and that will determine how many times a for() loop runs that will use a scanf() for Year[] and DataForYear[]. Year[] and DataForYear[] must use the same scanf() statement, which I know how to do.
After taking all of this, I need to create a function called output() which will basically printf() all of the values the user just gave me (being Year[] and DataForYear[]). However, I need a way to return both arrays to the main function, as well as NumberOfYears. I have heard this can be done using something called structs, however we have not learned this yet and I am sure my instructor will not approve.
The instructor's notes suggested that only NumberOfYears should be returned to the main function, but I simply do not see how this is possible. If you have a way that I can pass of these things along to output() without having to return them, that would be great to. Otherwise, I just need some sort of way to do this, and the rest of program is history.
Functions can return via their parameters as well as their return:
int doMyThing( int *somethingElse ) {
*somethingElse = 40;
return 2;
}
int main( void ) {
int a;
int b = doMyThing( &a );
printf( stdout, "%d\n", a + b ); // prints "42"
}
And you can extend that to arrays by using int **somethingElse, or whatever you need.
In C, anytime you pass an 'array', you actually pass a pointer. So your return type can be something like int*, depending on the type of the array you need to pass back. You can use &x to get the address of variable x (e.g. the first element of your array) if necessary.
The other possibility is that since you're passing a pointer you don't even need to actually return it. You can modify your array inside the input function and then see it's changed afterward in main without doing more work.

Resources