Re: Help with structure->char_string




Barry Schwarz wrote:
On 18 Sep 2006 05:08:18 -0700, "SP" <polifemo@xxxxxxxxxxx> wrote:

Joe Estock wrote:
SP wrote:
I am writing a function to read data into a structure, but I seem to be
messing up the character string in the fopen() call.
When the program runs it does not open the file and fopen() returns a
NULL value.
I put in a few fprintf() calls to verify that the file name & path is
being set properly.

?? am I referencing the structure element correctly in the freadpng()
function ??

Thanks
Sal Polifemo


file png.h
=======
struct png_file{
char *name; /* name of image file */
FILE *filep; /* file pointer */
char *type; /* image type identifier */
int max_row; /* total rows of image */
int max_col; /* total columns of image */
int max_val; /* max pixel value of image */
int **pixel; /* pointer to memory space to store
image */
};

file freadpng.c
==========
#include <stdio.h>
#include "png.h"

int freadpng( struct png_file * image)
{
FILE *file_ptr;
fprintf(stderr,"\n#### file name: %s\n", &image->name);

&image->name? I think you mean image->name here. &image is a pointer to
type char * (char **). I'm surprised that your compiler doesn't print a
diagnostic.


when I remove the & operator the program crashes with a segfault error

That is because your call to strcpy is also wrong.

Thanks for pinting this out, it has taken me a while to understand why
this was a problem.


file_ptr = fopen(image->name, "r");

the problem was fixed by adding the & operator

No the problem wasn't fixed. You simply replaced one kind of
undefined behavior with another. Apply the fixes people have told
you.


I went back and appyed the fixes and it all works now.

file_ptr = fopen(&image->name, "r");

If your compiler did not produce a diagnostic here, you have real
problems. fopen requires a char*. You provided a char**. There is
no implicit conversion between the two. This is a violation requiring
a diagnostic.


if( !file_ptr )
{
fprintf(stderr,"\n#### error opening file \n");
return -1;
}
fclose(file_ptr);
return 1;
}

file main.c
=======
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fgetdata.h"
#include "png.h"

int freadpng( struct png_file *image);

int main(int argc, char * argv[])
{
struct png_file image_in;
int result;

image_in.name = malloc( ( strlen(argv[1]) + 1 ) * sizeof
*image_in.name );

if(image_in.name)
{
strcpy(&image_in.name, argv[1]);

You need to remove this &. This also requires a diagnostic as
discussed above.

You want to copy the data into the area name points to, not into name
itself. This will also invoke undefined behavior whenever
strlen(argv1) >= sizeof(struc png_file). Even when it doesn't invoke
undefined behavior, it causes a memory leak since you have lost the
pointer to the allocated memory.

}
else
{
fprintf(stderr,"Failed to copy filename to variable");

You really should bail here. The next line will invoke UB if your call
to malloc fails.

}
fprintf(stderr," \n#### image.name_p: %s\n", &image_in.name);

Again, you specified that you are printing a string in the format
parameter. In this scenario &image_in.name is not a string.


result = freadpng( &image_in);
if( result == 1)
{
fprintf(stderr,"\n#### back in main.c ####\n");
}
else
{
fprintf(stderr," \n#### function call error\n");
}


return EXIT_SUCCESS;
}


I am using the gcc compiler if that makes any difference.

Nope. But you may want to invoke it in compliant mode with maximum
warning level.


Remove del for email

.



Relevant Pages

  • Re: Help with structure->char_string
    ... int freadpng(struct png_file * image) ... type char *. ... This will also invoke undefined behavior whenever ... you specified that you are printing a string in the format ...
    (comp.lang.c)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: Pointers on string members of structure
    ... because this just points memstr to a fixed string and it is undefined to try ... char memstrA; ... string array directly like this and how? ... struct or if the struct member points to the array. ...
    (microsoft.public.vc.language)
  • Re: Write to file
    ... Barney Rubble 123-789-4561 ... an edit in the found string and write the same file back out. ... struct list_node *next; ... char *data); ...
    (comp.lang.c)
  • [PATCH]a tar filesystem for 2.6.10-rc1-mm3
    ... +static int tarfs_readdir(struct file * filp, ... struct tarent *dir_tarent, *ent; ... +static int tarfs_readlink(struct dentry *dentry, char *buffer, int buflen) ...
    (Linux-Kernel)

Loading