• C

array of pointers problem

Hi,

i am trying to set an array of char* programatically inside  a while loop but everything is coming out
equalling the last value. Can anyone help me???

HERES the CODE:

void GetEthos(char* ethoList)
{
  FILE *fp;
  char line[130];

  fp = popen( "/sbin/ifconfig -a | grep -e \"^eth.*\"", "r"); // Get the list of available ethenet adapters on the machine
  int count = 0;
  char* name;
  while( fgets( line, sizeof ine, fp) )
  {
    name = strtok(line, " ");  // Use string tokenizer to fined the first string token in the line
    ethoList[count] = name;
    count++;
  }
}
cossy74Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

srinimsCommented:
hi you have to get use of pointer of pointers....

0
cossy74Author Commented:
srinims: great.... what do you mean. Showing me what i am doing wrong would be very helpful.
0
Infinity08Commented:
void GetEthos(char** ethoList)   /* <--  pass char** to function */
{
  FILE *fp;
  char line[130];

  fp = popen( "/sbin/ifconfig -a | grep -e \"^eth.*\"", "r"); // Get the list of available ethenet adapters on the machine
  int count = 0;
  char* name;
  while( fgets( line, sizeof ine, fp) )
  {
    name = strtok(line, " ");  // Use string tokenizer to fined the first string token in the line
    strcpy(ethoList[count], name);    /*  <--  use strcpy, because the local variable goes out of scope */
    count++;
  }
  pclose(fp);   /* <---  don't forget to close the stream */
}

Call like this :

    char ethoList[10][16];    /* <--  choose the bounds correctly !! */
    GetEthos(ethoList);
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

srinimsCommented:
array bound also has need to be known.. while passing bounded array..
the above code(specified by infinity), is not working properly for ansi compilers


please try this in your code..

#include <stdio.h>
#include <strings.h>

void retParray(char x1[5][100])
{
        //char** x1=(char[5][100])x2;
        printf("%X %X %X \n",x1,x1[0],x1[1]);

        strcpy(x1[0],"hai");
        strcpy(x1[1],"bye");
}

int main()
{
        char x[5][100];
        int i;
        strcpy(x[0],"iam");
        strcpy(x[1]," here");
        printf("%X %X %X \n",x,x[0],x[1]);
        retParray(x);
        for(i=0;i<2;i++)
        {
                printf("%s \n",x[i]);

        }
        return 0;
}

0
Infinity08Commented:
>> the above code(specified by infinity), is not working properly for ansi compilers
Yes, my mistake :) I meant to post this code, but got distracted half wat through :

 void GetEthos(char** ethoList)   /* <--  pass char** to function */
{
  FILE *fp;
  char line[130];

  fp = popen( "/sbin/ifconfig -a | grep -e \"^eth.*\"", "r"); // Get the list of available ethenet adapters on the machine
  int count = 0;
  char* name;
  while( fgets( line, sizeof ine, fp) )
  {
    name = strtok(line, " ");  // Use string tokenizer to fined the first string token in the line
    ethoList[count] = (char*) calloc(strlen(name) + 1, sizeof(char));
    strcpy(ethoList[count], name);    /*  <--  use strcpy, because the local variable goes out of scope */
    count++;
  }
  pclose(fp);   /* <---  don't forget to close the stream */
}

Call like this :

    char* ethoList[10] = { 0 };    /* <--  choose the bounds correctly !! */
    GetEthos(ethoList);
    /* don't forget to free the ethoList array elements */


Good catch, srinims !
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srinimsCommented:
yep,, it will work..
if any error occur means try to pass like this
GetEthos((char**)&ethoList);


char* ethoList[10] = { 0 };
--no need of initializtion also
you could use like this
char* ethoList[10];

0
Infinity08Commented:
>> GetEthos((char**)&ethoList);
This cast will be done implicitly. But it doesn't hurt to add it.

>> --no need of initializtion also
It can save you trouble afterwards. If GetEthos() places less than 10 strings in the array, then how will you detect that ? By initializing the array to 0 pointers, you can easily check for that like this :

    int i = 0;
    while (ethoList[i]) {
      /* use this device */
    }
0
Infinity08Commented:
Well, it's not my day apparently. The last loop should have been like this of course :

    int i = 0;
    while (ethoList[i]) {
      /* use this device */
      ++i;
    }
0
cossy74Author Commented:
Inifinity08: I am getting a error when compiling. It is complaining about the declaration of the list.

test635.c:119: error: variable-sized object âethoListâ may not be initialized
0
Infinity08Commented:
>> test635.c:119: error: variable-sized object âethoListâ may not be initialized

Can you show me the code you are using ?
0
cossy74Author Commented:
Infinity08: it happens when i try to initialise the array to equal {0}. I remove this an everything is fine.
0
Infinity08Commented:
From the error message, I see that you tried to initialize a dynamic array with { 0 }. That's not possible - only static arrays can be initialized like that :

    char* ethoList[10] = { 0 };    /* <--  choose the bounds correctly !! */

(code from earlier comment of mine)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.