Solved

array of pointers problem

Posted on 2007-03-26
12
317 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

792 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