Solved

array of pointers problem

Posted on 2007-03-26
12
314 Views
Last Modified: 2010-04-15
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++;
  }
}
0
Comment
Question by:cossy74
  • 6
  • 3
  • 3
12 Comments
 
LVL 3

Expert Comment

by:srinims
ID: 18798015
hi you have to get use of pointer of pointers....

0
 

Author Comment

by:cossy74
ID: 18798218
srinims: great.... what do you mean. Showing me what i am doing wrong would be very helpful.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18798250
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 3

Expert Comment

by:srinims
ID: 18798275
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
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18798291
>> 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
 
LVL 3

Expert Comment

by:srinims
ID: 18798313
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18798363
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18798369
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
 

Author Comment

by:cossy74
ID: 18804417
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18806085
>> test635.c:119: error: variable-sized object âethoListâ may not be initialized

Can you show me the code you are using ?
0
 

Author Comment

by:cossy74
ID: 18894339
Infinity08: it happens when i try to initialise the array to equal {0}. I remove this an everything is fine.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18895895
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

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question