Like Tree2Likes
  • 1 Post By nonoob
  • 1 Post By nonoob

Pointer Questions

This is a discussion on Pointer Questions within the C Programming forums, part of the General Programming Boards category; Can someone please tell me why rules is passing a warning from incompatible pointer type and why there is a ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    Pointer Questions

    Can someone please tell me why rules is passing a warning from incompatible pointer type and why there is a Segmentation Fault in the second for loop (the one with the y)

    Here is the code that actually pertains to my question, thanks in advance to all who help.

    Code:
    fractalDraw(int depthFract, double angle, double iAngle, char *axiom, char **rules, double *x, double *y);
    
    int main(){
            double *x, *y;
            bmp = bmp_create(300, 300, 32);
            for(*x = 0; *x < 300; *x = *x + 1){
                    for(*y = 0; *y < 300; *y = *y + 1){
                            bmp_set_pixel(bmp, *x, *y, pixelB); //assume this works correctly (it does)
                    }
            }
    
            int numFract, i, depthFract, numRules, j, count = 0;
            double angle, iAngle;
            char axiom[10], rules[123][10], tbr, rhs[10];
            scanf("%d", &numFract);
            for(i = 0; i < numFract; i++){
                    scanf("%d", &depthFract);
                    scanf("%lf", &angle);
                    scanf("%lf", &iAngle);
                    scanf("%s", &axiom);
                    scanf("%d", &numRules);
                    for(j = 0; j < numRules; j++){
                            scanf("%s -> %s",&tbr,&rhs);
                            strcpy(rules[tbr],rhs);
                    }
            }
    
            fractalDraw(depthFract, angle, iAngle, axiom, rules, x, y);
    
            bmp_save(bmp, "OUTPUT.BMP");
            bmp_destroy(bmp);
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    double *x, *y defines two pointers, pointing to doubles. You never allocated space for the doubles. And the pointers themselves have undefined values.
    Then when you use *x in the loops, you are using some undefined address pointing to who knows where.
    scanf("%s -> %s", tbr, rhs) <-- tbr and rhs must be char arrays.
    tgrieger likes this.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Damn, I'm tired and I missed that, thank you. As for the tbr/rhs, rhs is a char array and for some reason if I tried %c with tbr (only one char will ever be entered in that spot), it won't work correctly :/. Any idea as for the incompatible pointer type question about the variable rules while it's in fractalDraw?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Oh you only want single character for tbr? Then use %c. But rhs is already an array so %s is OK there.
    In the prototype for fractalDraw try char rules[][10]. The compiler needs to know how wide the array is.
    tgrieger likes this.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    I was unaware of that, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Pointer Questions
    By chris.r in forum C Programming
    Replies: 13
    Last Post: 06-04-2010, 09:40 PM
  2. Smart pointer questions
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2009, 01:54 PM
  3. Pointer questions
    By 1rwhites in forum C Programming
    Replies: 1
    Last Post: 10-20-2005, 09:55 AM
  4. 2 Quick Pointer Questions:
    By Krak in forum C++ Programming
    Replies: 7
    Last Post: 01-24-2003, 08:41 PM
  5. 2 Questions about POINTER and MEMORY.
    By L.O.K. in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 04:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21