Hi,
I am a basic learner in C language.Could u please provide me a good link or tutorial or explanation regarding "order of precedence".
I have the table of "order of precedence"........but needed some examples.
I am always confusing with them. like,
*ptr++=something;
*++ptr=something
i=i++................etc.
Thanks & Regards.

If you have a table displaying the relative precedence of the various operators and you are familiar with the concept of precedence, then it's pretty much a look up every time you want to do something until you have it memorized.

Minor nit, ~s.o.s~, the initial examples by the OP were expressions and not declarations.

I am a basic learner in C language.Could u please provide me a good link or tutorial or explanation regarding "order of precedence".
I have the table of "order of precedence"........but needed some examples.
I am always confusing with them. like,
*ptr++=something;
*++ptr=something
i=i++

And I don't really think this is a precedence issue either.

*ptr++=something; // Assign 'something' to the value at which 'ptr' currently points, then increment 'ptr'.
*++ptr=something // Increment 'ptr' and then assign 'something' to that value at which 'ptr' now points
i=i++ // undefined behavior, do not use

In the first one, post increment happens after -- hence it is post increment.
In the second pre increment happens before -- hence it is pre increment.

I don't really know a shortcut. Just practice with them and it either sticks in your head or you find a way that is easier to follow mentally.

Minor nit, ~s.o.s~, the initial examples by the OP were expressions and not declarations.

Oh didn't read the question properly, assumed that it dealt with declarations -- thanks for pointing out the blunder.

Anyways considering he is a beginner, the link is a good read nonetheless. ;)

After a bit of a distraction, an example "practice" might begin with a skeleton like this.

#include <stdio.h>

int main(void)
{
   int array[] = {1,2,3,4,5,6,7,8,9};
   int *ptr;
   size_t i;

   puts("Show initial array.");
   for ( i = 0; i < sizeof array / sizeof *array; ++i )
   {
      printf("array[%lu] = %d\n", (long unsigned)i, array[i]);
   }

   puts("Demonstrate expression.");
   ptr = &array[2];
   printf("before: ptr = %p, *ptr = %d\n", (void*)ptr, *ptr);
   *ptr++ = 42;
   printf("after:  ptr = %p, *ptr = %d\n", (void*)ptr, *ptr);

   puts("Show current array.");
   for ( i = 0; i < sizeof array / sizeof *array; ++i )
   {
      printf("array[%lu] = %d\n", (long unsigned)i, array[i]);
   }

   puts("Demonstrate expression.");
   ptr = &array[5];
   printf("before: ptr = %p, *ptr = %d\n", (void*)ptr, *ptr);
   *++ptr = -5;
   printf("after:  ptr = %p, *ptr = %d\n", (void*)ptr, *ptr);

   puts("Show current array.");
   for ( i = 0; i < sizeof array / sizeof *array; ++i )
   {
      printf("array[%lu] = %d\n", (long unsigned)i, array[i]);
   }
   return 0;
}

/* my output
Show initial array.
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 7
array[7] = 8
array[8] = 9
Demonstrate expression.
before: ptr = 0022FF48, *ptr = 3
after:  ptr = 0022FF4C, *ptr = 4
Show current array.
array[0] = 1
array[1] = 2
array[2] = 42
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 7
array[7] = 8
array[8] = 9
Demonstrate expression.
before: ptr = 0022FF54, *ptr = 6
after:  ptr = 0022FF58, *ptr = -5
Show current array.
array[0] = 1
array[1] = 2
array[2] = 42
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = -5
array[7] = 8
array[8] = 9
*/
Comments
very clear example, nice one! -Niek

@ Dave Sinkula,
That little bit of sample code is going to be very helpful to me, now that I'm using pointers. Thank you!.
However I noticed that the array is a int; how do you do when is a array of chars. I'm struggling with strings. I don't know how I can use a pointer to fill an array of char or string.

This a sample of what I don't understand.
Could you give me some Pointers?.

/*
 * string_ptr.c
 * test of pointer action
 */
#include <stdio.h>

int main(void)
{
    char  string[60];   /* array of chars */
    char *string_ptr;   /* a pointer to a string??? */

    string_ptr = string; /* initialition of string pointer??? */
    string_ptr = "This is a string of less than 60 chars"; /* assign this string to the pointer */

    printf(string);   /* display the character array */
    putchar('\n');
    printf(string_ptr); /* display the string pointer */
    putchar('\n');
    
    return(0);
}
/* string display output is garbage */
/* string_ptr pointer is correct */
/* why? */

I'm on the phone, so first a postmortem...

#include <stdio.h>

int main(void)
{
   char  string[60];   /* array of chars (uninitialized = garbage) */
   char *string_ptr;   /* a pointer */

   string_ptr = string; /* point to the uninitialized garbage */
   string_ptr = "This is a string of less than 60 chars";  /* [B]re-[/B]point to a string literal */

   printf(string);   /* display the character array  (uninitialized garbage) */
   putchar('\n');
   printf(string_ptr); /* display the string literal */
   putchar('\n');

   return 0;
}

[edit]An adaptation of my earlier post for strings my be like this.

#include <stdio.h>
 
int main(void)
{
   char array[] = "abcdefghijkl";
   char *ptr;
 
   puts("Show current array.");
   printf("array: \"%s\"\n", array);
 
   puts("Demonstrate expression.");
   ptr = &array[2];
   printf("before: ptr = %p, *ptr = '%c'\n", (void*)ptr, *ptr);
   *ptr++ = 'X';
   printf("after:  ptr = %p, *ptr = '%c'\n", (void*)ptr, *ptr);

   puts("Show current array.");
   printf("array: \"%s\"\n", array);

   ptr = &array[2];
   puts("Demonstrate expression.");
   ptr = &array[5];
   printf("before: ptr = %p, *ptr = '%c'\n", (void*)ptr, *ptr);
   *++ptr = 'Q';
   printf("after:  ptr = %p, *ptr = '%c'\n", (void*)ptr, *ptr);

   puts("Show current array.");
   printf("array: \"%s\"\n", array);
   return 0;
}
 
/* my output
Show current array.
array: "abcdefghijkl"
Demonstrate expression.
before: ptr = 0022FF62, *ptr = 'c'
after:  ptr = 0022FF63, *ptr = 'd'
Show current array.
array: "abXdefghijkl"
Demonstrate expression.
before: ptr = 0022FF65, *ptr = 'f'
after:  ptr = 0022FF66, *ptr = 'Q'
Show current array.
array: "abXdefQhijkl"
*/

Minor nit, ~s.o.s~, the initial examples by the OP were expressions and not declarations.
And I don't really think this is a precedence issue either.

*ptr++=something; // Assign 'something' to the value at which 'ptr' currently points, then increment 'ptr'.
*++ptr=something // Increment 'ptr' and then assign 'something' to that value at which 'ptr' now points
i=i++ // undefined behavior, do not use

In the first one, post increment happens after -- hence it is post increment.
In the second pre increment happens before -- hence it is pre increment.

I don't really know a shortcut. Just practice with them and it either sticks in your head or you find a way that is easier to follow mentally.

I am trying to solve some complex expressions related to pointers......that surly deals with "order of precedence"...
like a small simple example..
++*ptr=20; wt is the result.....?
so in this way i tried for good link in google but dint get.....
now iam k vth declarations....could u people giv me a good link that deals vth complex expressions
thanks & regards.

isnt it BODMAS?

Brackets
Powers (the o means of)
Division
Multiplication
Addition
Subtraction

wt about the other symobls......like unary +,-,.,*............etc

@ Dave Sinkula,

string_ptr = string; /* point to the uninitialized garbage */
string_ptr = "This is a string of less than 60 chars"; /* re-point to a string literal */

This is the trouble part of my code then. I thought once I pointed the
first time to the char array I could assign the string using the pointer.

I guess what I would like to know..., is there a way I could
change the value in the string array using the pointer?. Or I will always
need to initialize the array with the string?.

I guess what I would like to know..., is there a way I could
change the value in the string array using the pointer?. Or I will always
need to initialize the array with the string?.

I'm not Dave, but I'll try to help ;)

How I see it, the pointer is pointing to the first byte in a chunk of allocated memory - in this case, 60 bytes. Obviously if you try to assign a string the address of a literal, it's going to change what it was originally pointing at.

So the only way to modify the original string with a pointer is to manually change each charecter with the * operator:

*string_ptr = 'a'; // for example

Or use some of the C standard functions for manipulating your string.

I'm not Dave, but I'll try to help ;)

Thank you Joeprogrammer, I think I get it now. This would never work:

*string_ptr = "This is a string";

because I will be trying to fit a whole array of chars inside just one
char which is the first address that the pointer is pointing to, correct?

Thank you Joeprogrammer, I think I get it now. This would never work:

*string_ptr = "This is a string";

because I will be trying to fit a whole array of chars inside just one
char which is the first address that the pointer is pointing to, correct?

Yep, that's pretty much how I understand it.

I am trying to solve some complex expressions related to pointers......that surly deals with "order of precedence"...
like a small simple example..
++*ptr=20; wt is the result.....?
so in this way i tried for good link in google but dint get.....
now iam k vth declarations....could u people giv me a good link that deals vth complex expressions
thanks & regards.

This will be my last response to any question in the form, "now iam k vth...". Make an effort to post a question, and I may make an effort to post a reply.

So Operator Precedance...

++*ptr=20;

Well, ++ and * are at the same precedance, higher than = . So, we've got *ptr which is preincremented. Then you attempt to assign 20 to the result of preincrementing *ptr , which is a syntax error.

Frankly, running that by your compiler is likely to get you that information within seconds; asking on a forum apparently gets you results in hours. Would it not be just a wee bit more fruitful to take my advice and try developing code and when you get results or errors that confuse you, then post the code and ask a thoughtful question. Throwing darts about like this is really going make it take a long time to learn the language.

I guess what I would like to know..., is there a way I could change the value in the string array using the pointer?. Or I will always need to initialize the array with the string?.

Actually there are many ways to change value of a character array using a pointer to it...

// Untested !!!

int main( void )
{
    int i = 0 ;
    // first way -- initialize while declaring
    char arr[BUFSIZ] = { '\0' } ;

    char* ptr_arr = arr ;

    // second way -- use string copy
    strcpy( ptr_arr, "Hello" ) ;
    puts( ptr_arr ) ;

    // Third way -- use fgets( ) or any other function which accepts user input
    puts( "Enter the string: ") ;
    fgets( ptr_arr, BUFSIZ, stdin ) ;
    puts( ptr_arr ) ;

    // And many other ways like looping through the entire array
    // and setting up individual characters
    putchar( '\n' ) ;
    for( i = 0; i < BUFSIZ - 1; ++i )
        ptr_arr[ i ] = '0' ;
    ptr_arr[ i ] = '\0' ;   // add a null terminator at the end
    puts( ptr_arr ) ;


    getchar( ) ;
    return 0 ;
}
Comments
Thanks for doing my cleanup/followup. I was meaning to get around to it... --Dave

This will be my last response to any question in the form, "now iam k vth...". Make an effort to post a question, and I may make an effort to post a reply.

So Operator Precedance...

++*ptr=20;

Well, ++ and * are at the same precedance, higher than = . So, we've got *ptr which is preincremented. Then you attempt to assign 20 to the result of preincrementing *ptr , which is a syntax error.

Frankly, running that by your compiler is likely to get you that information within seconds; asking on a forum apparently gets you results in hours. Would it not be just a wee bit more fruitful to take my advice and try developing code and when you get results or errors that confuse you, then post the code and ask a thoughtful question. Throwing darts about like this is really going make it take a long time to learn the language.

I vl follow u............

This will be my last response to any question in the form, "now iam k vth...". Make an effort to post a question, and I may make an effort to post a reply.

I vl follow u............

You don't seem to get it do you?

@ Dave Sinkula,


This is the trouble part of my code then. I thought once I pointed the
first time to the char array I could assign the string using the pointer.

I guess what I would like to know..., is there a way I could
change the value in the string array using the pointer?. Or I will always
need to initialize the array with the string?.

I think what you're saying is that you may be used to languages other than C in which you can assign a string a value using the = operator. C is at a lower level and this is not what happens. Adding pointers further confuses the issue because it can make things appears as doing just that.

Arrays cannot be assigned using = in C. Instead strings are copied using strcpy .

I hope I've touched on the heart of the matter. Please continue with any followup questions.

This question has already been answered. Start a new discussion instead.