All Questions
Tagged with pass-by-reference c
552 questions
4
votes
3
answers
188
views
why does getaddrinfo() take the node, service and hints arguments as pointers? [duplicate]
int getaddrinfo(const char *node, // e.g. "www.example.com" or IP
const char *service, // e.g. "http" or port number
const struct addrinfo *...
0
votes
2
answers
147
views
Why does this function not modify the original array when passed as a parameter?
#include <stdio.h>
void modifyArray(int arr[]) {
arr = (int[]){6, 7, 8, 9, 10}; // Trying to modify the array
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
modifyArray(arr);
for (...
2
votes
2
answers
106
views
Issues passing by reference with gcc. No issues when compiling with gcc
EDIT: Adding this to the top because it is critical information being missed. The FSCC_REGISTERS_INIT call does EXACTLY what it is supposed to do, which is fill the entire struct with '-1's. The ...
2
votes
3
answers
99
views
Pass "Slice" structs by value or by reference? [closed]
I have the following two structs. They are both "slices" in that they have a pointer to data and dimensions, but they do not "own" the data (they are not responsible for memory ...
1
vote
1
answer
57
views
Error in using call by reference with function to change the head of a singly linked list
I'm learning linked lists in C, and have come across an error when trying to add a value at beginning of a linked list using functions
#include<stdio.h>
#include<stdlib.h>
struct node{
...
2
votes
3
answers
158
views
Why pass void pointer instead of custom type?
Setup
I'm taking over code from another person. I found a coding pattern that seems odd to me. This is for an embedded 32 bit PIC processor, in case that's relevant.
They have a custom typedef in a ...
2
votes
1
answer
119
views
How to implement singleton object in C?
I have a requirement for an OBJECT to be used by other objects. The requirement is that the OBJECT not require initialization by any external call. So I began with something like so...
// .h
struct ...
0
votes
0
answers
25
views
When passing a pointer to a function in c, what actually happens? [duplicate]
I am trying to learn linked lists. In this I need to use a double pointer to pass by reference. But I am wondering, when we pass a pointer to the function, aren't we sending a copy of the address to ...
0
votes
2
answers
83
views
Why cant I modified the char array when passing it as an argument to the function and assigning it a new value?
void changeName(char* name) {
//other action
//strcpy(name, "String"); -- this working
name = "Marcus"; // -- this not working
}
int main() {
char name[10] ...
1
vote
4
answers
106
views
char * gives garbage value when pointing to a variable of a function in C
When i pass a pointer to a function and then change the pointer to a another location of the memory, I get SIGSEV or garbage values. Where is a code to demonstrate that:
#include <stdio.h>
void ...
0
votes
1
answer
90
views
C Changing value of array of struct through reference
I asked a similar question before, when I didnt know what was the problem of my code. Just as I was recommended I will give it in a better format.
This is an example of what happens to my code.
#...
1
vote
2
answers
91
views
It seems like my function is only receiving the first char of a string when being called [closed]
I am building a basic registration feature for a school project and keep getting an error when I try to check the username and password from a database. The idea is to have a user type in their login ...
0
votes
1
answer
56
views
Why my print statement inside the swap function is giving such output?
#include <stdio.h>
void swap(int* x, int* y){
int temp;
temp = *x;
*x = *y;
*y = temp;
printf("The value of x and y inside the function is x = %d and y = %d", x, y);...
2
votes
2
answers
121
views
why do I need to add an & sign while allocating a memory space inside a functions? [duplicate]
I wanted to try making an allocate function for a cell in a list, but when using it inside another functions, I need to add an "&" sign
I am aware of what "&" means in c (...
0
votes
1
answer
55
views
C Array elements all changing to the same value
I'm trying to populate an array of names using an input file, however the elements of my array all end up turning into the same value and I'm unsure why.
char* names[name_count];
for (int i = ...