copying arrarys into arrarys 
Author Message
 copying arrarys into arrarys

I am trying to copy variable locations of an arrary
into another arrary and getting a access violation
during the strcpy function.

Would some kind soul show me the correct way to make
the prototype pass the arrary name correctly.
Code works if i change ary[i] to comments[i].
I just cannot pass in the arrary name correctly.

------------ cut on dotted line  -----------------------

#include <string.h>                /* strcpy,...                          */
#include <stdlib.h>                /* malloc,.....                        */
#include <stdio.h>                 /* standard in/output                  */

static  char    iheader[5][20] ;
static  char    comments[5][20] ;
static  int     i ;
static  int     lint_rtn ;
static  char    *lint_ptr ;

static void copy_section (char ary[], int start_line, int max_lines) ;

int main (int argc, char *argv[])
{
        lint_rtn = sprintf (iheader[0], "Line 0,0") ;
        lint_rtn = sprintf (comments[0], "comm 0,0") ;
        lint_rtn = sprintf (comments[1], "comm 1,0") ;
        lint_rtn = sprintf (comments[2], "comm 2,0") ;
        lint_rtn = sprintf (comments[3], "comm 3,0") ;
        lint_rtn = sprintf (comments[4], "comm 4,0") ;

        copy_section (comments[0], 0, 5) ;

        for (i=0; i <= 5; i++)
          printf ("%s\n", iheader[i]) ;

        return (0) ;

Quote:
} /* end main */

static void  copy_section (char ary[], int start_line, int max_lines)
        /******************************************************************
        *
        *       copy_section - copies ary into form arrary at starting line
        *
        ******************************************************************/
{
        int     i,t ;

        t = start_line ;

        for (i=0; i <= max_lines; i++)
        {
          lint_ptr = strcpy (iheader[t], (char *)ary[i]) ;
          t++ ;
        }

Quote:
} /* end copy_section */

--
While Alcatel may claim ownership of all my ideas (on or off the job),
Alcatel does not claim any responsibility for them. Warranty expired when u
opened this article and I will not be responsible for its contents or use.


Sat, 21 Apr 2001 03:00:00 GMT  
 copying arrarys into arrarys
[snip]
Quote:
>static void  copy_section (char ary[], int start_line, int max_lines)
[snip]
>{
> int i,t ;

> t = start_line ;

> for (i=0; i <= max_lines; i++)
> {
>   lint_ptr = strcpy (iheader[t], (char *)ary[i]) ;

That char * cast is a lie.  It's not nice to lie to your compiler.
[snip]
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Sat, 21 Apr 2001 03:00:00 GMT  
 copying arrarys into arrarys
But I get
junk.c", line 43: warning: improper pointer/integer combination: arg #2

lint_ptr = strcpy (iheader[t], ary[i]) ;

: [snip]
: >static void  copy_section (char ary[], int start_line, int max_lines)
: [snip]
: >{
: > int i,t ;
: >
: > t = start_line ;
: >
: > for (i=0; i <= max_lines; i++)
: > {
: >   lint_ptr = strcpy (iheader[t], (char *)ary[i]) ;
: That char * cast is a lie.  It's not nice to lie to your compiler.
: [snip]
: --
: Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
: C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
: Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
: Want Software?  Algorithms?  Pubs? http://www.infoseek.com

--
While Alcatel may claim ownership of all my ideas (on or off the job),
Alcatel does not claim any responsibility for them. Warranty expired when u
opened this article and I will not be responsible for its contents or use.



Sun, 22 Apr 2001 03:00:00 GMT  
 copying arrarys into arrarys
John Strange:
Quote:
>But I get
>junk.c", line 43: warning: improper pointer/integer combination: arg #2

>lint_ptr = strcpy (iheader[t], ary[i]) ;

Exactly.  That's what it *is*.  By performing the cast, you are telling the
compiler to interpret this thing as something it's not.

--
C-FAQ ftp sites: ftp://ftp.eskimo.com ftp://rtfm.mit.edu
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ Book: ISBN 0-201-84519-9.
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Mon, 23 Apr 2001 03:00:00 GMT  
 copying arrarys into arrarys

Quote:

> But I get
> junk.c", line 43: warning: improper pointer/integer combination: arg #2

> lint_ptr = strcpy (iheader[t], ary[i]) ;

Hi John Strange,

Your original code is a bit strange indeed. I can't figure out what
is is actually meant to do. But I can explain tha problem with the
above statement: "iheader" is a 2D array of char, whereas "ary" is
only a pointer to char. If you dereference both with "[i]" the result
is that "iheader[t]" is a pointer to char (and therefore a correct
destination for "strcpy()"), the "ary[i]" is one simple "char" !
So please ask yourself: what is the source string you want to copy
from ? Simply casting "ary[i]" to "char*" is nonsense. You can
not sensibly convert a single chatacer (like "A") into a pointer
to char. This is a the more confusing part of your code.

Another solid bug that I saw in your code was:
  for (i=0; i <= max_lines; i++)

In C arrays are indexed starting at 0 up to and including (n-1) for
n elements. This means that an array with 5 elements is indexed from
0 to 4. But your loop counts from 0 to 5 and you will be writing
into a non existing array element.

You should mainly try to resolve the confusion surrounding your
"copy_section" function. Clearly decide what the source string
is and what the destination string is. And remember that in C a
string is represented by a zero terminated array of characters.

Stephan
(initiator of the campaign against grumpiness in c.l.c)

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Mon, 23 Apr 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Shallow copy or deep copy for System.Array?

2. Copying arrays, copying structs

3. Cannot copy file over read-only copy

4. Copy Constructor : Copying Base Class Member Variables

5. Printing - how to detect which copy on multiple copies

6. Referenced of Referenced dll are copied even if copy local is set to false.

7. Assemblies referenced by the referenced assemblies are copied even if copy local is set to false.

8. Copying a bitmap from the clipboard (my code isn't creating a proper copy?!)

9. Copy a map using 'stl::copy' ?

10. How do I create a copy of a DataTable that copies all the data too?

11. Icon to Bitmap, which icon size being copied?

12. Newbie Question: Copying HTML content to the clip board

 

 
Powered by phpBB® Forum Software