Sponsored Content
Top Forums Programming Problem with array of pointers Post 302384861 by Corona688 on Wednesday 6th of January 2010 01:33:12 PM
Old 01-06-2010
You are storing the string in the same buffer every time you fgets, overwriting anything previously read, and every pointer in your array just points to this same buffer. Without seeing the code it's only a guess, but is that buffer a local variable? It doesn't even exist except when the function it's in is being called, and at other times may be used for other memory, hence the further corruption. You need to store the data globally, not the pointer, so, a global array of strings:

Code:
#include <stdio.h>

extern char str_table[512][512];

int main(void)
{
  int line=0;
  while(fgets(str_table[line], 512, stdin) != NULL)
  {
    line++;
    if(line >= 512)
    {
      fprintf(stderr, "Too many lines\n");
      return(1);
    }
  }
}

Or if you wanted to just store pointers, you could use strdup:

Code:
#include <string.h>
#include <stdio.h>

extern char *str_table[512];

int main(void)
{
  int line=0;
  char buf[512];
  while(fgets(buf, 512, stdin) != NULL)
  {
    str_table[line]=strdup(buf);  // Create a persistent copy of the string in heap memory
    line++;
    if(line >= 512)
    {
      fprintf(stderr, "Too many lines\n");
      return(1);
    }
  }

  line--;

  while(line >= 0)
  {
    free(str_table[line]);  // Get rid of the persistent copies of strings
    line--;
  }
}


Last edited by Corona688; 01-06-2010 at 02:39 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

Pointers and array

hi all, let say i have a pointer exit, and this exit will store some value. how can i store the value that the pointer points to into an array and then print them out from the array. thanks in advance (2 Replies)
Discussion started by: dianazheng
2 Replies

2. Shell Programming and Scripting

array problem

Dear Experts, please help me out once again my array concepts is not very clear i have one text file like. 1|usa|hh 2|usa|ll 3|usa|vg 4|uk|nn 5|uk|bb 6|kuwait|mm 6|kuwait|jkj 7|dubai|hh i want to store the third fied of a text file in he array and after that it should give me some... (6 Replies)
Discussion started by: shary
6 Replies

3. Programming

Problem with pointers, structures, and Pthread

Hello all, I'm working on a small wrapper library for a bigger project, and i've been killing my self over (what I think is) a pointer problem. Here is the code (I extracted the part of the code where the problem is for better reading, I tested the code below, and I get the same problem):... (13 Replies)
Discussion started by: tmp0
13 Replies

4. Shell Programming and Scripting

Array problem

I am using /bin/ksh for this problem. I have created some arrays with variable names as the array names: cnt=1 { while read myline; do tempmeas="${meas%%;*}" cto="${meas#*;}" tempstream=$stream # wholemeas holds the name of the array # each array name... (0 Replies)
Discussion started by: ajgwin
0 Replies

5. Programming

Help on some array problem!!

i have no idea how to make a text file abc efg hij klm nop qrs to be a array such as, arr to be "abc efg" arr "hij kml" etc..... in C (2 Replies)
Discussion started by: tyckelvin1
2 Replies

6. Programming

Traversing in Array of pointers

Please find the below program. the requirement and description of the program also given: ganesh@ubuntu:~/my_programs/c/letusc/chap9$ cat fa.c.old /* Program : write a program to count the number of 'e' in thefollowing array of pointers to strings: char *s = { "We will teach you how... (12 Replies)
Discussion started by: ramkrix
12 Replies

7. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

8. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

9. Programming

Pointers and array

Hello, I read from a book exercise for a challenge. How to print out each letter of char array a by two different pointers pa and ppa in the example? I have tried my code for letter "r" by testing without full understanding as only the first one worked. #include<stdio.h> int main() { char... (17 Replies)
Discussion started by: yifangt
17 Replies

10. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (3 Replies)
Discussion started by: spflanze
3 Replies
funcolumnactivate(3)						SAORD Documentation					      funcolumnactivate(3)

NAME
FunColumnActivate - activate Funtools columns SYNOPSIS
#include <funtools.h> void FunColumnActivate(Fun fun, char *s, char *plist) DESCRIPTION
The FunColumnActivate() routine determines which columns (set up by FunColumnSelect()) ultimately will be read and/or written. By default, all columns that are selected using FunColumnSelect() are activated. The FunColumnActivate() routine can be used to turn off/off activa- tion of specific columns. The first argument is the Fun handle associated with this set of columns. The second argument is a space-delimited list of columns to activate or de-activate. Columns preceded by "+" are activated and columns preceded by a "-" are de-activated. If a column is named with- out "+" or "-", it is activated. The reserved strings "$region" and '$n' are used to activate a special columns containing the filter region value and row value, respectively, associated with this row. For example, if a filter containing two circular regions is specified as part of the Funtools file name, this column will contain a value of 1 or 2, depending on which region that row was in. The reserved strings "$x" and "$y" are used to activate the current binning columns. Thus, if the columns DX and DY are specified as binning columns: [sh $] fundisp foo.fits[bincols=(DX,DY)] then "$x" and "$y" will refer to these columns in a call to FunColumnActivate(). In addition, if the activation string contains only columns to be activated, then the routine will de-activate all other columns. Simi- larly, if the activation string contains only columns to de-activate, then the routine will activate all other columns before activating the list. This makes it simple to change the activation state of all columns without having to know all of the column names. For example: o "pi pha time" # only these three columns will be active o "-pi -pha -time" # all but these columns will be active o "pi -pha" # only pi is active, pha is not, others are not o "+pi -pha" # same as above o "pi -pha -time" # only pi is active, all others are not o "pi pha" # pha and pi are active, all others are not o "pi pha -x -y" # pha and pi are active, all others are not You can use the column activation list to reorder columns, since columns are output in the order specified. For example: # default output order fundisp snr.ev'[cir 512 512 .1]' X Y PHA PI TIME DX DY -------- -------- -------- -------- --------------------- -------- -------- 512 512 6 7 79493997.45854475 578 574 512 512 8 9 79494575.58943175 579 573 512 512 5 6 79493631.03866175 578 575 512 512 5 5 79493290.86521725 578 575 512 512 8 9 79493432.00990875 579 573 # re-order the output by specifying explicit order fundisp snr.ev'[cir 512 512 .1]' "time x y dy dx pi pha" TIME X Y DY DX PI PHA --------------------- -------- -------- -------- -------- -------- -------- 79493997.45854475 512 512 574 578 7 6 79494575.58943175 512 512 573 579 9 8 79493631.03866175 512 512 575 578 6 5 79493290.86521725 512 512 575 578 5 5 79493432.00990875 512 512 573 579 9 8 A "+" sign by itself means to activate all columns, so that you can reorder just a few columns without specifying all of them: # reorder 3 columns and then output the rest fundisp snr.ev'[cir 512 512 .1]' "time pi pha +" TIME PI PHA Y X DX DY --------------------- -------- -------- -------- -------- -------- -------- 79493997.45854475 7 6 512 512 578 574 79494575.58943175 9 8 512 512 579 573 79493631.03866175 6 5 512 512 578 575 79493290.86521725 5 5 512 512 578 575 79493432.00990875 9 8 512 512 579 573 The column activation/deactivation is performed in the order of the specified column arguments. This means you can mix "+", "-" (which de- activates all columns) and specific column names to reorder and select columns in one command. For example, consider the following: # reorder and de-activate fundisp snr.ev'[cir 512 512 .1]' "time pi pha + -x -y" TIME PI PHA DX DY --------------------- -------- -------- -------- -------- 79493997.45854475 7 6 578 574 79494575.58943175 9 8 579 573 79493631.03866175 6 5 578 575 79493290.86521725 5 5 578 575 79493432.00990875 9 8 579 573 We first activate "time", "pi", and "pha" so that they are output first. We then activate all of the other columns, and then de-activate "x" and "y". Note that this is different from: # probably not what you want ... fundisp snr.ev'[cir 512 512 .1]' "time pi pha -x -y +" TIME PI PHA Y X DX DY --------------------- -------- -------- -------- -------- -------- -------- 79493997.45854475 7 6 512 512 578 574 79494575.58943175 9 8 512 512 579 573 79493631.03866175 6 5 512 512 578 575 79493290.86521725 5 5 512 512 578 575 79493432.00990875 9 8 512 512 579 573 Here, "x" and "y" are de-activated, but then all columns including "x" and "y" are again re-activated. Typically, FunColumnActivate() uses a list of columns that are passed into the program from the command line. For example, the code for funtable contains the following: char *cols=NULL; /* open the input FITS file */ if( !(fun = FunOpen(argv[1], "rc", NULL)) ) gerror(stderr, "could not FunOpen input file: %s ", argv[1]); /* set active flag for specified columns */ if( argc >= 4 ) cols = argv[3]; FunColumnActivate(fun, cols, NULL); The FunOpen() call sets the default columns to be all columns in the input file. The FunColumnActivate() call then allows the user to con- trol which columns ultimately will be activated (i.e., in this case, written to the new file). For example: funtable test.ev foo.ev "pi pha time" will process only the three columns mentioned, while: funtable test.ev foo.ev "-time" will process all columns except "time". If FunColumnActivate() is called with a null string, then the environment variable FUN_COLUMNS will be used to provide a global value, if present. This is the reason why we call the routine even if no columns are specified on the command line (see example above), instead of calling it this way: /* set active flag for specified columns */ if( argc >= 4 ){ FunColumnActivate(fun, argv[3], NULL); } SEE ALSO
See funtools(7) for a list of Funtools help pages version 1.4.2 January 2, 2008 funcolumnactivate(3)
All times are GMT -4. The time now is 09:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy