c - Return statement in function never prints to screen -


i'm trying pass in parameters 1 function, store values set of elements in struct. print values within struct, calling function.

here's i'm trying do:

#include <stdio.h> #include <string.h> #include <stdlib.h>  typedef struct temp {     int num;     char * name;     struct temp * nextptr; }temp;  temp * test(); char * printtest(temp * print);  int main (void) {     test("tv",200);     struct temp * t;     printtest(t);     return 0; }  temp * test(char * name, int num) {     struct temp * mem = malloc(sizeof(struct temp));     mem->name = malloc(sizeof(strlen(name) + 1));     mem->name = name;      mem->num = num;     mem->nextptr = null;     return mem; }  char * printtest(temp * print) {     char * output;      output = malloc(sizeof(struct temp));     print = malloc(sizeof(struct temp));      sprintf(output,"it's name %s, , costs %d",print->name,print->num);     return output; //should print "tv" , costs "200" } 

the function printtest, doesn't seem print out anything, rather if hardcore printf within it, prints null values , zero. however, tried print struct values in test function, work after initializing values.

for example, if printf("%d",mem->num); print out value of 200(in test function).

but sprintf , return combination in last function doesn't result in same result. appreciated!

you're not capturing value return test, therefore gets lost:

int main (void) {     //changed capture return value of test.     struct temp * t = test("tv",200);     printtest(t);     return 0; } 

also print function wrong:

// points struct allocated in test. char * printtest(temp * print) {     char * output;      // don't want size of temp here, want size     // of formatted string enough room members of      // struct pointed temp.     output = malloc(sizeof(struct temp));      // you're not using this.     print = malloc(sizeof(struct temp));      // sprintf buffer pointing nothing, output buffer     // writing past memory allocated.     sprintf(output,"it's name %s, , costs %d",print->name,print->num);      // return doesn't print anything, returns value     // function signature specifies, in case char *     return output; //should print "tv" , costs "200" } 

try this, you'll take pointer allocated, , use printf write formatted string stdout:

// we're not returning void printtest(temp * print ){     if (temp == null ){         // nothing print, leave.         return;     }      printf("it's name %s, , costs %d",print->name,print->num);     return; } 

also notes test function:

// char pointer string, invocation in main, // string stored in static read memory temp * test(char * name, int num) {     // malloc memory struct, good.     struct temp * mem = malloc(sizeof(struct temp));      // malloc space length of string want store.     mem->name = malloc(sizeof(strlen(name) + 1));     // here's bit of issue, mem->name pointer, , name pointer.     // you're doing here assigning pointer name variable      // mem->name, you're not copying string - since      // invoke method static string, nothing happen ,     // string passed in, , you'll able reference -     // leaked memory allocated mem->name above.     mem->name = name;      // num not apointer, it's value, therefore it's okay assign     // this.     mem->num = num;     mem->nextptr = null;     return mem; } 

try instead:

temp * test(char * name, int num) {     struct temp * mem = malloc(sizeof(struct temp));      // still malloc room name, storing pointer returned malloc     // in mem->name     mem->name = malloc(sizeof(strlen(name) + 1));     // now, however, we're going *copy* string stored in memory location     // pointed char * name, memory location allocated,      // pointed mem->name     strcpy(mem->name, name);      mem->num = num;     mem->nextptr = null;     return mem; } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -