Thread: Just some pointer explanation I need

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    67

    Just some pointer explanation I need

    Hi!

    Code:
     
      char *c;
      c="Hello!";
    Im just wondering , howcome the above stattemensts are correct. And if you printf c it would actually print "Hello" . THe pointer *c is not pointing anywhere, so it's NULL, the c is a variable so the statment is correct, but where does that variable come from?? This is confuzing me. What happens when pointer is created? Does pointer actually makes two variables, a pointer variable and a normal variable?

    Regards,
    Swan

  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
    You basically have
    Code:
    char anonymously_generated_by_compiler[] = "hello";
    char *c;
    c = anonymously_generated_by_compiler;
    The string exists in memory somewhere (only the compiler knows for sure where that is), and then you set a pointer to point to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    So if I understand you correctly , first the string is made by a compiler somewhere in the memmory and then the asignment takes place.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, the assignment being a pointer to the first location.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    I hoipe I can ask another question here. I want to make a function witch changes the values of two strings.

    Code:
    #include<stdio.h>
    
    void switchem(char *p1, char *p2);
     int main()
      {
     
     char niz1[]="Niz1";
     char niz2[]="Niz2";
     printf(" Niz1: %s  Niz2: %s\n", niz1,niz2);
     
     switchem(&niz1,&niz2);
    printf(" Niz1: %s  Niz2: %s\n", niz1,niz2);
     return 0;
    
     }
    
     void switchem(char *p1, char *p2){
      char *temp;
       temp=p2;
       p2=p1;
       p1=temp;
       
       
      }
    I want the niz1="Niz2" and niz2="Niz1". I want to do this with pointers. But I get . I get an error saying: passing arg1 and arg2 to the method switchem from incompatible pointer type.

  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
    > I get an error saying: passing arg1 and arg2 to the method switchem from incompatible pointer type.
    Well that's because you did
    switchem(&niz1,&niz2);

    Using & on an array results in a pointer to the array (not a pointer to a char)
    switchem(niz1,niz2);
    Would make the compiler happy, but then you'd need to fix the function as well.

    At the moment, you only swap over copies of the pointers. You need to use strcpy() if you want to swap over what those two pointers point to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Thanks for help Salem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM