-1

I'm new to C / pointers / memory management and am having trouble implementing a few functions for a project I'm working on.

In my builtins.c file, I have a function called printalias that is called to print all the alias names and corresponding values stored in my program. At the end, I want to print one of the alias names by retrieving it via another function called getal.

int x_printalias(int nargs, char *args[]) {
  int i = 0;
  // Loop through, print names and values
  for(i = 0; i< 100; i++)
  {
    if(alias_names[i][0]!='\0' && !alias_disabled[i])
    {
      char * var = alias_names[i];
      char * val = alias_vals[i];
      fprintf(stderr,"%s = %s\n", var, val );
    }
  }
  // This is where I want to retrieve the string from another function
  char * hello = "brett";
  hello = getal(hello);
  fprintf(stderr,"Got alias for brett --> %s",hello);
  return 0;
}

My getal function exists in my shellParser.c file and looks like this, generally performing the same looping and returning when it is found:

const char * getal(int nargs, char *args[])
{
  fprintf(stderr,"\nRetrieving alias...\n");
  int i = 0;
  fprintf(stderr, "check1\n" );

  fprintf(stderr,"Got args[0]: %s\n", args[0]);

  while (alias_names[i][0]!='\0' && i < MAX_ALIAS_LENGTH ) // Find empty slot in variables array
  {
    fprintf(stderr, "check2\n" );

    fprintf(stderr,"I is currently %i and current varible in slot is %s\n",i,alias_names[i]);
    //strncpy(hello, variables[i], MAX_VAR_LENGTH);  // Variable at current slot
    if(strcmp(alias_names[i], args[0]) == 0) // If we have an entry, need to overwrite it
    {
      fprintf(stderr,"Found  alias %s = %s at spot %i\n",args[0],alias_vals[i], i); // Not at end if here
      return alias_vals[i];
    }
    i++;
  }
  fprintf(stderr, "check3\n" );

  // Elided....

  return '\0';
}

In the end of my printalias function, I want to test that this getal function is working by calling it on a hardcoded string "brett". However, when I call my printalias function from the command line, it makes it to the "Check 1" print statement and then simply quits without error or return value.

I think this has something to do with my memory management or incorrect declaration of variables with pointers. Can anybody spot something (or a lot of things) that I'm doing wrong here?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Ladybro
  • 756
  • 2
  • 9
  • 30
  • 3
    You call the `getal` function with one argument of type `char*` and your definition of this functions says that it needs two arguments of types `int` and `char*[]`. – Wojtek Surowka Apr 07 '15 at 23:24
  • If you want to use variable arguments in `getal`, you will need to `#include ` and declare `const char *getal(int,...)`. The ellipsis tells the compiler that the function accepts variable arguments. Use `va_start()` to initialize the `variable` you want such as `args` in your case. And use `va_end()` to cleanup used memory. – alvits Apr 08 '15 at 00:18
  • possible duplicate of [Returning a string from a function in C](http://stackoverflow.com/questions/9378998/returning-a-string-from-a-function-in-c) – Mgetz Apr 08 '15 at 01:49

2 Answers2

0

You must declarete list of argument for to call getal and it call with these list. And pointer of return values getal must be const char*

  //....
    // This is where I want to retrieve the string from another function
      char * hello[] = {"brett"}; // this list argument for getal function
      const  char *strGetal; 
      strGetal = getal(1,hello);
      fprintf(stderr,"Got alias for brett --> %s",strGetal);
      return 0;
    }
-1

Example:

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


char** get_all(int argc, char **argv)
{
    char *value;
    char **values = NULL;
    int i;

    values = (char**) malloc(sizeof (char) * argc);
    if (values == NULL) {
        perror("malloc");
        return NULL;
    }


    for (i = 0; i < argc; i++, argv++) {
        value = strchr(*argv, ':');
        values[i] = (value + 1);
    }

    return values;
}

int main()
{
    char *args[] = {"key:a", "key:b", "key:c"};
    char **values;
    int i;

    values = get_all(3, args);

    for (i = 0; i < 3; i++) {
        puts(values[i]);
    }

    return 0;
}