Help - Array of pointers to strings dynamically declared 
Author Message
 Help - Array of pointers to strings dynamically declared

Hi! I need to read inputs from keyboard (strings), and then dynmically
sizing a variable to copy what I read Into. After I have to crate an array
of pointers to these strings, for sort them.
What I have done untile now seems to work, but when I test this and I try to
read these strings from the array of pointers, I've got no my strings, but
other things... I thing there's an error and what I read is the address of
the memory where my strings are. This is my code, I hope you understand my
english and my problem.
Thanks to all!!

#include <stdio.h>
#include <stdlib.h>
void read_strings(char * a[], int n)
{
   int i = 0;
   char buffer[100];     //for reading input before I know the size
saranno lunghe
   char temp[1];     //after I will resize it!
   for (i = 0; i<n; i++){
        printf("\nInsert input: ");
        gets(buffer);
    temp=realloc(temp, strlen(buffer));     // sesizing to real size
    temp = buffer;    // I copy the buffer into
    a[n] = &temp;  // here I "should" link my string at the array
   }

Quote:
}

int main(int argc, char *argv[])
{
    char* a[100];
    read_string(a,10);
    int j=0;
    for (j=0;j<10;j++){
        printf("\nn=%d --> %s",j,*a[j]);
    /* here the problem! what I read is not what I inserted!!!!! */
    }

    system("PAUSE");
    return 0;

Quote:
}

I pray you help me if you can!
Hi,
Gio from Italy

--



Sun, 20 Nov 2005 03:47:58 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:

> Hi! I need to read inputs from keyboard (strings), and then dynmically
> sizing a variable to copy what I read Into. After I have to crate an array
> of pointers to these strings, for sort them.
> What I have done untile now seems to work, but when I test this and I try to
> read these strings from the array of pointers, I've got no my strings, but
> other things... I thing there's an error and what I read is the address of
> the memory where my strings are. This is my code, I hope you understand my
> english and my problem.
> Thanks to all!!

> #include <stdio.h>
> #include <stdlib.h>
> void read_strings(char * a[], int n)
> {
>    int i = 0;
>    char buffer[100];     //for reading input before I know the size
> saranno lunghe
>    char temp[1];     //after I will resize it!
>    for (i = 0; i<n; i++){
>         printf("\nInsert input: ");
>         gets(buffer);
>     temp=realloc(temp, strlen(buffer));     // sesizing to real size
>     temp = buffer;    // I copy the buffer into
>     a[n] = &temp;  // here I "should" link my string at the array
>    }
> }

> int main(int argc, char *argv[])
> {
>     char* a[100];
>     read_string(a,10);
>     int j=0;
>     for (j=0;j<10;j++){
>         printf("\nn=%d --> %s",j,*a[j]);
>     /* here the problem! what I read is not what I inserted!!!!! */
>     }

>     system("PAUSE");
>     return 0;
> }

> I pray you help me if you can!
> Hi,
> Gio from Italy

#include <stdio.h>
#include <stdlib.h>

void read_strings( char **a, int n );

void
read_strings(char **a, int n)
{
    int i = 0;
    char buffer[100];
    for( i = 0; i<n; i++ ){
       printf("\nInsert input: ");
       fgets(buffer, sizeof(buffer),stdin);
       a[i] = malloc(strlen(buffer));
       memcpy( a[i], buffer, strlen(buffer));
    }

Quote:
}

int
main(int argc, char *argv[])
{
     int j = 0;
     char **a;
     a = malloc(100*sizeof(char*));
     read_strings(a,10);

     for (j=0;j<10;j++){
       printf("\nn=%d --> %s",j, a[j]);
     }
     /* system("PAUSE"); */
     for(j=0; j<10; j++){
       char *tmp = a[j];
       if(tmp)
         free(tmp);
     }
     free(a);
     printf("done.\n");
     exit(0);

Quote:
}

cheers,
Jeff


Sun, 20 Nov 2005 04:15:35 GMT  
 Help - Array of pointers to strings dynamically declared

Hey Gio, I'm not a C guru but I can help push this along (I hope).

Quote:

> #include <stdio.h>
> #include <stdlib.h>
> void read_strings(char * a[], int n)
> {
>    int i = 0;
>    char buffer[100];     //for reading input before I know the size
>    char temp[1];     //after I will resize it!

temp is a character array of size 1, but I belive you want it
to be a pointer, so use:
char *temp;

Quote:
>    for (i = 0; i<n; i++){
>         printf("\nInsert input: ");
>         gets(buffer);
>     temp=realloc(temp, strlen(buffer));     // sesizing to real size

I guess you could use realloc here, but why not use malloc, as
each string entered represents a seperate invocation of the
function read_strings:
temp = malloc(strlen(buffer) + 1); // use +1 as strlen doesn't count '\0'

Quote:
>     temp = buffer;    // I copy the buffer into

Then copy the string across with strcpy:
strcpy(temp, buffer);

finally, assign your new memory space:

a[n] = temp;

I haven't read your main routine and don't know much about
reading strings with scanf so I leave that to others if
it needs correction.

--
Ben Fitzgerald
London, UK



Sun, 20 Nov 2005 04:17:43 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:


>> Hi! I need to read inputs from keyboard (strings), and then dynmically
>> sizing a variable to copy what I read Into. After I have to crate an
>> array
>> of pointers to these strings, for sort them.
>> What I have done untile now seems to work, but when I test this and I
>> try to
>> read these strings from the array of pointers, I've got no my strings,
>> but
>> other things... I thing there's an error and what I read is the
>> address of
>> the memory where my strings are. This is my code, I hope you
>> understand my
>> english and my problem.
>> Thanks to all!!

>> #include <stdio.h>
>> #include <stdlib.h>
>> void read_strings(char * a[], int n)
>> {
>>    int i = 0;
>>    char buffer[100];     //for reading input before I know the size
>> saranno lunghe
>>    char temp[1];     //after I will resize it!
>>    for (i = 0; i<n; i++){
>>         printf("\nInsert input: ");
>>         gets(buffer);
>>     temp=realloc(temp, strlen(buffer));     // sesizing to real size
>>     temp = buffer;    // I copy the buffer into
>>     a[n] = &temp;  // here I "should" link my string at the array
>>    }
>> }

>> int main(int argc, char *argv[])
>> {
>>     char* a[100];
>>     read_string(a,10);
>>     int j=0;
>>     for (j=0;j<10;j++){
>>         printf("\nn=%d --> %s",j,*a[j]);
>>     /* here the problem! what I read is not what I inserted!!!!! */
>>     }

>>     system("PAUSE");
>>     return 0;
>> }

>> I pray you help me if you can!
>> Hi,
>> Gio from Italy

> #include <stdio.h>
> #include <stdlib.h>

> void read_strings( char **a, int n );

> void
> read_strings(char **a, int n)
> {
>    int i = 0;
>    char buffer[100];
>    for( i = 0; i<n; i++ ){
>       printf("\nInsert input: ");
>       fgets(buffer, sizeof(buffer),stdin);
>       a[i] = malloc(strlen(buffer));
>       memcpy( a[i], buffer, strlen(buffer));
>    }
> }

What about the string terminator? ;-)

--
boa

libclc home: http://libclc.sourceforge.net



Sun, 20 Nov 2005 04:18:09 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:
> finally, assign your new memory space:

> a[n] = temp;

> I haven't read your main routine and don't know much about
> reading strings with scanf so I leave that to others if
> it needs correction.

> --
> Ben Fitzgerald
> London, UK

Ok, thanks to all.
But I have a problem! When my function ends, in the MAIN I retrieve this a[]
array of pointers.
How can I print values?
printf("%s",a[i]) --> run-time error
printf("%s",*a[i]) --> run-time error

won't receive them correctly)
Where my error is?
How can I sort my array if I can't see my strings???


Sun, 20 Nov 2005 05:28:58 GMT  
 Help - Array of pointers to strings dynamically declared
Yes, my problem is the same.. It's about 4 hours that I'm working on it..
Inside my function I can see a[i] with printf and it works.. I read my
string.
But if I try this printf in the main, with the a[] that return from the
function, it doesn't work!!
It's incredible..
argh!


Sun, 20 Nov 2005 08:51:44 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:

> Hi! I need to read inputs from keyboard (strings), and then dynmically
> sizing a variable to copy what I read Into. After I have to crate an array
> of pointers to these strings, for sort them.

  <http://cbfalconer.home.att.net/download/ggets.zip>

and look at the usage demos.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Sun, 20 Nov 2005 12:57:02 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:

> Yes, my problem is the same.. It's about 4 hours that I'm working on it..
> Inside my function I can see a[i] with printf and it works.. I read my
> string.
> But if I try this printf in the main, with the a[] that return from the
> function, it doesn't work!!
> It's incredible..
> argh!

Check this program .It works for me.
------------------------------------
#include <stdio.h>
#include <stdlib.h>
void read_strings(char * a[], int n)
{
   int i = 0;
   char buffer[100];     //for reading input before I know the size
   char *temp;     //after I will resize it!
   for (i = 0; i<n; i++){
        printf("\nInsert input: ");
        gets(buffer);
    temp=malloc(strlen(buffer)+1);     // sesizing to real size
    strcpy(temp,buffer);    // I copy the buffer into
    a[i] = temp;  // here I "should" link my string at the array
   }

Quote:
}

int main(int argc, char *argv[])
{
    char *a[100];
    int j=0;
    read_strings(a,10);
    for (j=0;j<10;j++){
        printf("\nn=%d --> %s",j,a[j]);
    /* here the problem! what I read is not what I inserted!!!!! */
    }

    return 0;

Quote:
}



Sun, 20 Nov 2005 15:09:51 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:



>> Yes, my problem is the same.. It's about 4 hours that I'm working on it..
>> Inside my function I can see a[i] with printf and it works.. I read my
>> string.
>> But if I try this printf in the main, with the a[] that return from the
>> function, it doesn't work!!
>> It's incredible..
>> argh!

> Check this program .It works for me.

Checking now.

Quote:
> ------------------------------------
> #include <stdio.h>
> #include <stdlib.h>
> void read_strings(char * a[], int n)
> {
>    int i = 0;
>    char buffer[100];     //for reading input before I know the size

If you have a C99 compiler, this comment is fine, but the lack of a
prototype for strcpy (later in the program) is not. If you have a C90
compiler, you can get away with the strcpy, just, but then this comment is
a syntax error.

Quote:
>    char *temp;     //after I will resize it!
>    for (i = 0; i<n; i++){
>         printf("\nInsert input: ");
>         gets(buffer);

How were you planning to stop an ignorant or malicious user from overflowing
your buffer?

Quote:
>     temp=malloc(strlen(buffer)+1);     // sesizing to real size

If this call should fail, your next function call will write to a NULL
pointer.

Quote:
>     strcpy(temp,buffer);    // I copy the buffer into

No prototype in scope for strcpy.

"It works" is a little generous, I think.

<snip>

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 20 Nov 2005 15:57:20 GMT  
 Help - Array of pointers to strings dynamically declared

Quote:


> > Hi! I need to read inputs from keyboard (strings), and then dynmically
> > sizing a variable to copy what I read Into. After I have to crate an array
> > of pointers to these strings, for sort them.
> > What I have done untile now seems to work, but when I test this and I try to
> > read these strings from the array of pointers, I've got no my strings, but
> > other things... I thing there's an error and what I read is the address of
> > the memory where my strings are. This is my code, I hope you understand my
> > english and my problem.
> > Thanks to all!!

> > #include <stdio.h>
> > #include <stdlib.h>
> > void read_strings(char * a[], int n)
> > {
> >    int i = 0;
> >    char buffer[100];     //for reading input before I know the size
> > saranno lunghe
> >    char temp[1];     //after I will resize it!
> >    for (i = 0; i<n; i++){
> >         printf("\nInsert input: ");
> >         gets(buffer);

As noted, gets() is vulnerable to buffer overflow, and
fgets(), or something more complicated like CBFalconer's
ggets(), is better.  But this is not likely your current problem.

Quote:
> >     temp=realloc(temp, strlen(buffer));     // sesizing to real size

This is illegal; you can't assign to an array, plus you can't
realloc() a declared variable, only space that was dynamically
allocated by malloc, calloc, or a previous realloc.

Quote:
> >     temp = buffer;    // I copy the buffer into

Again you can't assign to an array.

Quote:
> >     a[n] = &temp;  // here I "should" link my string at the array

This is illegal because &temp is char(*)[100] and a[n] is char*;
even if the compiler proceeds with the (trivial) conversion,
you are probably setting all the a[i] to point to the stack space
allocated for the local temp, which is deallocated by the time
the caller uses these pointers to try to access it, invalidly.

Quote:
> >    }
> > }

> > int main(int argc, char *argv[])
> > {
> >     char* a[100];
> >     read_string(a,10);

Why declare 100 pointers and then (try to) read values
for only 10 of them?

- Show quoted text -

Quote:
> >     int j=0;
> >     for (j=0;j<10;j++){
> >         printf("\nn=%d --> %s",j,*a[j]);
> >     /* here the problem! what I read is not what I inserted!!!!! */
> >     }

> >     system("PAUSE");
> >     return 0;
> > }

> > I pray you help me if you can!
> > Hi,
> > Gio from Italy

> #include <stdio.h>
> #include <stdlib.h>

> void read_strings( char **a, int n );

> void
> read_strings(char **a, int n)
> {
>     int i = 0;
>     char buffer[100];
>     for( i = 0; i<n; i++ ){
>        printf("\nInsert input: ");

For maximum portability, fflush(stdout).

Quote:
>        fgets(buffer, sizeof(buffer),stdin);

Better, but should check for EOF (or I/O error).

Quote:
>        a[i] = malloc(strlen(buffer));
>        memcpy( a[i], buffer, strlen(buffer));

strlen(buffer)+1 on both.  Or strcpy() for the latter.  
Or nonstandard strdup().  Should check for
failure (null returned) from malloc, or strdup.

Quote:
>     }
> }

> int
> main(int argc, char *argv[])
> {
>      int j = 0;
>      char **a;
>      a = malloc(100*sizeof(char*));

This doesn't need to be dynamic; if it is, should
check for malloc failure, although if I would relax
this rule anywhere it would be a test driver like this.

Quote:
>      read_strings(a,10);

>      for (j=0;j<10;j++){
>        printf("\nn=%d --> %s",j, a[j]);
>      }
>      /* system("PAUSE"); */
>      for(j=0; j<10; j++){
>        char *tmp = a[j];
>        if(tmp)
>          free(tmp);

Don't need an extra variable, or the test;
free(a[j]) is safe even if a[j] is null.

Quote:
>      }
>      free(a);
>      printf("done.\n");

This will run together with the last data line output.

Quote:
>      exit(0);
> }

> cheers,
> Jeff

- David.Thompson1 at worldnet.att.net


Sun, 27 Nov 2005 14:03:58 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. declaring array of strings - help!

2. Need help declaring an array of strings

3. URGENT HELP in declaring arrays of strings

4. declaring an array of pointers to malloced arrays

5. Copy Array string to another Pointer array string in ANSI-C

6. HELP!: declaring pointer to CArray of pointers

7. Newbie: Declaring and Accessing Array of Integer Pointers

8. Beginner needs help with strings, pointers, arrays

9. declaring an array of pointers

10. declaring an array of function pointers?

11. need help with array of string pointers

12. Declaring an array of pointers in Visual Basic (and putting and getting data from it)

 

 
Powered by phpBB® Forum Software