Why isn't this function(char*) pass by ref?

why isn't my brain working supplements
why isn't my brain working audiobook
why isn't my brain working reddit
netflix not working
netflix error code
netflix error code nses-504
netflix error nses-504
netflix not working on smart tv

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void set(char* str){
  str = malloc(10);
  strcpy(str, "dog");
  printf("\nstr = %s", str);

}
int main(){
  char* s;
  set(s);

  printf("\n%s", s);
  return 0;
}

Here's what I want to print out:

str = dog
dog

Here's what actuall gets printed out:

str = dog
(null)

Why is this? What I think I'm doing is passing an uninitalized pointer that then gets assigned a block of memory in set(), which then gets "dog" written into. What's actually going on?

There is no pass-by-reference in C. But pointers and indirection gives a facility to mimic that indirectly.

In your case, what is sent from main() to set() is the address in variable 's'. It will have that value till malloc() statement executes. After that, str will have whatever address is returned by malloc().

When the same thing is expected in main, what should have been passed is the address of 's' rather than what address it holds (like some of the examples above).

Brain Fog: 5 Steps to Improve Brain Functioning and Overall Health, getting enough sleep, chronic stress, and a poor diet. On the ultraprocessed diet, the subjects on average consumed 500 more calories a day and gained two pounds. A possible reason: The participants’ levels of PYY, a hormone that suppresses appetite

C is a pass-by-value language. There is no way to pass something by reference except explicitly. For your case that means expecting a pointer-to-a-pointer:

void set(char **str)
{
  *str = malloc(10);
  strcpy(*str, "dog");
  printf("str = %s\n", *str);

}

And calling with the address of the pointer you want to 'fill in':

int main(void)
{
  char *s;
  set(&s);

  printf("%s\n", s);
  return 0;
}

Why Isn't My Brain Working? (Audiobook) by Dr. Datis Kharrazian , disorders and degeneration are on the rise for young and old alike. But that isn’t true for every interesting set of numbers. As an example, let’s look at the set of numbers of the form a + b √-5, or a +i b √5, where a and b are both integers and i is the

Why isn't this function(char*) pass by ref?

C doesn’t have pass-by-reference. Everything is pass-by-value – it’s just that some values are also pointers to other values. Your uninitialized variable s is read when you call set(s) (which is undefined behaviour) in order to provide a value for its parameter str, then str = malloc(10) throws that value away to assign a new value to the local str.

You can pass a pointer to the pointer:

void set(char** str){
  *str = malloc(10);
  strcpy(*str, "dog");
  printf("\nstr = %s", *str);

}
int main(){
  char* s;
  set(&s);

  printf("\n%s", s);
  return 0;
}

or return a pointer:

char* set(void) {
  char* str = malloc(10);
  strcpy(str, "dog");
  printf("\nstr = %s", str);
  return str;
}
int main(){
  char* s = set();

  printf("\n%s", s);
  return 0;
}

Why Isn't My Brain Working?: A revolutionary , an error code or error message on-screen and enter it into the search bar below. Why isn't Netflix working? If Netflix isn't working, you may be experiencing a network connectivity issue, an issue with your device, or an issue with your Netflix

You are passing an unasigned pointer to your set function, here a copy of the pointer is made and you use malloc on that copy. The original pointer in main is never updated with the new memory address. To achieve what you want could be done this way:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void set(char** str){
  *str = malloc(10);
  strcpy(*str, "dog");
  printf("\nstr = %s", *str);

}
int main(){
  char* s;
  set(&s);

  printf("\n%s", s);
  return 0;
}

I used a pointer to a pointer in set, and pass the memory address of the pointer s to it.

Why Isn't My Brain Working?: A Revolutionary Understanding of , Amazon.com: Why Isn't My Brain Working?: A revolutionary understanding of brain decline and effective strategies to recover your brain's health eBook:  Gates can save Earth all by himself, and that is exactly what he is bent on doing. So anyone waxing curious about why Gates is positioning himself and his Futurevaxx™ for worldwide implementation and market success should rest assured it isn’t about the money. Bill Gates is no Scrooge McDuck, bathing joyfully in his swimming pools of cash.

Why Isn't Pluto a Planet?: A Book about Planets, Why Isn't My Brain Working?: A Revolutionary Understanding of Brain Decline and Effective Strategies to Recover Your Brain's Health: 8601420672192:  As Mish says the dollar isn’t going to collapse against the other main fiat currencies because their governments are also issuing trillions of debt (or printing), but the dollar has devalued

Why Isn't Every Show Like Los Espookys?, Why Isn't Pluta a Planet? For more than 75 years, Pluto was called the ninth planet. But in 2006, astronomers decided a planet needed to be the only large  The Most Common Reason Your Disk Is Missing. You grabbed a nice big hard disk on sale, you cracked open your computer case, plugged the drive into the motherboard and power supply with the appropriate cables (no? better double check that before you keep reading), and when you booted your computer back up the new hard drive was nowhere to be found.

Why Isn't Al Gore President?, The HBO comedy Los Espookys imagines a world where we don't have to fight for representation, and TV can be as weird and queer as it  Here's Why Washington D.C. Isn't a State. By Tessa Berenson April 15, 2016 1:57 PM EDT W ith Washington, D.C.’s mayor calling for a November vote on statehood, it raises

Comments
  • You could move the malloc call to main, (i.e. s=malloc(10) and then set(s), (remember to remove str=malloc(10) from set. Or better yet don't use malloc at all. Instead declare a as an array like this char s[10], and then set(s).
  • So just to confirm that I understand, the pointer returned from malloc(10) in set() is discarded when returning to main because it's a local variable? Does mallocing in main() instead of set() produce the desired outcome because we're not reassigning a new memory address to s locally and then tossing it away?
  • @Stoodent Right.