Problem with array of pointers


 
Thread Tools Search this Thread
Top Forums Programming Problem with array of pointers
# 1  
Old 01-06-2010
Question Problem with array of pointers

Hi All,

I am using the array of pointers and storing the address of string.This is a global list.
So i am using extern to give the reference of this list to another file and using reading the data from this string.
But list is being corrupted and string is missing some characters in string. eg.

In file A:

Code:
char *str[512]; 
char line[512];  

while(fgets(line, 512, fp) != NULL) str[count++] = line;

in file B:

Code:
char* alert; 
for(i = 0 ; i < count; count++) alert = str;

Anyone can suggest me why it is being corrupted.that would be appreciated. It's crashing also in gcc for linux and but not solaris.

Thanks!

Last edited by Franklin52; 01-06-2010 at 02:48 PM.. Reason: Adding code tags and reformat code and text
# 2  
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..
# 3  
Old 01-07-2010
Thank you!
I will use it.

Last edited by lovevijay03; 01-07-2010 at 02:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question