Thread: printing the contents the pointer points to

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    29

    Angry printing the contents the pointer points to

    Code:
      
    
    sprintf((char *)magval, "P%c", ch);
      printf("magic no: %s\n", *magval);
    What am I doing wrong here?

    magval is declared as char *magval (in sprintf()my compiler seems to want the cast for some reason)

    But ultimately I want to print the contents that magval points to. How do I do this?

    This is really ticking me off right now.
    Disk space: the final frontier

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > sprintf((char *)magval, "P%c", ch);
    My guess is, you didn't allocate any space

    > (in sprintf()my compiler seems to want the cast for some reason)
    Which one would that be?

    > printf("magic no: %s\n", *magval);
    To print a string, use
    printf("magic no: %s\n", magval);

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    29
    > sprintf((char *)magval, "P%c", ch);
    My guess is, you didn't allocate any space
    >I have, magval is a member in a structure

    > (in sprintf()my compiler seems to want the cast for some reason)
    Which one would that be?
    >gcc (via MinGW) - some settings in my compiler I need to check

    > printf("magic no: %s\n", *magval);
    To print a string, use
    printf("magic no: %s\n", magval);
    >This works, but need to check it's in the member variable.
    Disk space: the final frontier

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    you are trying to access magval as if it is a regular variable, if it is a member of a structure, you need to access it like

    structurename.magval

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by cblix
    > sprintf((char *)magval, "P%c", ch);
    My guess is, you didn't allocate any space
    >I have, magval is a member in a structure
    What Salem is saying is that magval has to point to something. If not then you are using an uninitialized pointer. magval should be set to the address of some character buffer/array or the return value from some malloc call prior to you writing/reading from it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If magval is a member of a struct, how did this ever compile?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    29
    Quote Originally Posted by John_
    you are trying to access magval as if it is a regular variable, if it is a member of a structure, you need to access it like

    structurename.magval
    Yeah that is the point at the moment, so I can later assign to the member of the structure.
    Disk space: the final frontier

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    29
    Hopefully this helps:

    Code:
    typedef struct Image {
    	char magval;
    	int width;
    	int height;
    	int maxval;
    	char *data;
    } Image;
    
    void Q_readPPMHeader(FILE *fp, char magval, int *width, int *height, int *maxval)
    {
      char ch;
     
    
      if ((fscanf(fp, "P%c\n", &ch) != 1) && (ch != '6' || ch != '5' || ch !='3' || ch != '2')) 
    	Q_error("Cannot read either .PPM or .PGM file");
      
      sprintf(magval, "P%c", ch); /**ISSUE IS HERE**/
      printf("magic no: %s\n", magval);/**ISSUE IS HERE**/
    
    
      /* skip comments */
      ch = getc(fp);
      while (ch == '#') {
    	do {
    		ch = getc(fp);
    	} while (ch != '\n');	/* read to the end of the line */
    		  ch = getc(fp);            
        }
    
      if (!isdigit(ch)) Q_error("cannot read header information from ppm file");
    
      ungetc(ch, fp);		/* put that digit back */
    
      /* read the width, height, and maximum value for a pixel */
      fscanf(fp, "%d%d%d\n", width, height, maxval);
    
      if (*maxval != 255) Q_error("image is not true-color (24 bit); read failed");
    
     
      printf("width:    %d\n", *width);
      printf("height:   %d\n", *height);
      printf("maxval:   %d\n", *maxval);
    }
    
    Image *Q_ImageRead(char *filename)
    {
      int width, height, maxval, size;
      char magval; /**ISSUE IS HERE**/
    
    
      Image *image = (Image *) malloc(sizeof(Image));
      FILE  *fp    = fopen(filename, "r");
    
      if (!image) Q_error("cannot allocate memory for new image");
      if (!fp)    Q_error("cannot open file for reading");
    
      Q_readPPMHeader(fp, &magval, &width, &height, &maxval);
    
      size = width * height;
     
      image->magval = magval;
      image->width  = width;
      image->height = height;
      image->maxval = maxval;
      image->data   = (char *) malloc(size);
      
      if (!image->data) Q_error("cannot allocate memory for new image");
    
      fread((char *)image->data, 1, (size_t) size, fp);
      
      fclose(fp);
    
      return image;
    }
    Last edited by cblix; 01-27-2006 at 10:05 AM.
    Disk space: the final frontier

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You can't write a multi-character string into a single character. You don't print a single character with a string specifier.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. printing array contents
    By c_programmer in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 06:00 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Pointer points to struct pointer
    By gogo in forum C Programming
    Replies: 4
    Last Post: 11-27-2001, 12:14 PM