Thread: need help with pointers

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    need help with pointers

    the first function works (int length (char a[]), but the output to the second function is -28865. I need to use pointers and pointer arithmatic.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int length (char a[]);
    int point_size (int *a);
    
    int main()
    {
    char word[]="Trying this out";
    int size;
    int pointer_eh;
    
    size = length (word);
    
    printf("The length is %d\n", size);
    
    printf("The length is %d", pointer_eh);
    
    
    return 0;
    }
    
    
    int length (char a[])
    {
    	int i;
    	int size=0;
    
            clrscr();
    
            for (i = 0; i != strlen(a); i++)
    	{
    		size++;
    	}
    	return(size);
    }
    
    int point_size (int *a)
    {
    
    	int i, pointer_eh;
    
    	while (*a[i] != '\0')
    	{
    		*(a+1);
    	 	i++;
    	}
    	return (pointer_eh);
    }
    Last edited by digy; 08-14-2003 at 08:43 AM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if you need to use pointer math, you can eliminate all your little "++" operations that you're doing on an integer. You need to do those on a pointer instead. Additionally, use a forward slash not a back slash to end your code tags
    edit: you got it already!
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    16

    thanks

    Yah, I can never figure out one way or another..lol
    But the program still doesnt work...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    point_eh in main is never given a value, so what you're doing is printing whatever happens to be at that memory location when the program is run. I assume that point_size is the pointer notation equivalent of length, so this would be what you want:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int length (char a[]);
    int point_size (char *a);
    
    int main()
    {
            char word[]="Trying this out";
            int size;
            int pointer_eh;
    
            size = length (word);
            pointer_eh = point_size (word);
    
            printf("The length is %d\n", size);
            printf("The length is %d\n", pointer_eh);
    
            return 0;
    }
    
    
    int length (char a[])
    {
            int i;
            int size=0;
    
            /*clrscr();*/
    
            for (i = 0; i != strlen(a); i++)
            {
                    size++;
            }
            return(size);
    }
    
    int point_size (char *a)
    {
    
            int i = 0, pointer_eh;
    
            while (*(a+i) != '\0')
            {
                    *(a+1);
                    i++;
            }
            return (pointer_eh);
    }
    Last edited by Prelude; 08-14-2003 at 09:39 AM.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    Hi,
    Always remember to intialise i as it is using some junk value at this moment in this function.
    int point_size (char *a)

    int i, pointer_eh;

    while (*(a+i) != '\0')

    *(a+1);
    i++;

    return (pointer_eh);

    secondly, can u tell me what r u exactly doing with this func.
    what does this statement *(a+1); do??????????
    thx
    gs

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    Hi,
    Always remember to intialise i as it is using some junk value at this moment in this function.
    #define brs /* i donno y the flw br. is not making me sending the msgs */
    #define bre
    int point_size (char *a) br

    int i, pointer_eh;

    while (*(a+i) != '\0') br

    *(a+1);
    i++;

    return (pointer_eh);bre bre

    secondly, can u tell me what r u exactly doing with this func.
    what does this statement *(a+1); do??????????
    thx
    gs

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    now the function point_size is coming out
    The length is 0

    and I am not sure what *(a+1); does

    actually I just took it out and the same answer comes up...lol

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    if a is a pointer to a char, then *(a+1) is a "dereferenced" pointer to the next char in memory. So it is the char that follows *a. Get it?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    what exactly does dereference mean

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    9
    that means if a is at address 100 then a+1 points to 101 as a is pointer to char.
    now *(a+1) will access its contents at 101.
    this statement doesnt mean anything unless u do something with this.
    if u r cal. size then do this

    int point_size (char *a)

    int pointer_eh=0;

    while (*a++) /* u can also do without dereferencing it.
    pointer_eh++;

    return (pointer_eh);

    gs

  11. #11
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    Code:
    int point_size (char *a)
    {
    
            int i = 0, pointer_eh;
    
            while (*(a+i) != '\0')
            {
                    *(a+1);
                    i++;
            }
            return (pointer_eh);
    }
    what value has pointer_eh?
    Undeclared, because you dont assign it any value.
    "Can i really learn this? If i cant, why bother?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM