Home » questions » Another challenging question regarding C,C++ ...?

Another challenging question regarding C,C++ ...?

2006-08-14 06:03:42, Category: Programming & Design
int *ptr = a; // ok int *ptr=&a[0]; // ok scanf("%s",a); // ok int *ptr=&a; // error ...why ? (while ..) scanf("%s",&a); // ok why ? This is not any code segment. They r seprate statements. Use them in ur program and then tell me why scanf("%s",&a); is allowed while char *ptr=&a; is not here ... a is a string array. (char a[5];)

Answers

  1. Web-designer ©

    On 2006-08-14 06:11:50


    Try to visit http://www.pixel2life.com it might help...because i am not that much fimiliar to C++
  2. Big H

    On 2006-08-14 06:32:57


    Okay so if this is pretty much exact code. The first problem I would say is that you are re-declaring the same varable name. And that is illegal in C/C++. If you want to simple change the value of *ptr then you need not put the int in front of it after the first declaration. However if you just wrote statements that you did not understand then I will say... assuming a is a character array which is appears to be. If you want to assign the address of that array to a point you need not use the &. I believe by default the address of an array is passed around and not the value. so that is why int *ptr=&a errors out. Secondly scanf works because well... I don't have a good answer for you.... it's beena while since i really dived into C. But i know its got to be the way scanf handles its arguments. Hopefully this helped a little bit.
  3. griz803

    On 2006-08-14 10:02:13


    Let's go through this line by line. int *ptr=a; /* NOT really ok, C will let you, most good compilers will complain. If the warning flags in the compiler are set to strong settings, then you get a warning saying that the type of a is incompatible with the type of ptr. Which is true if a is declared as a character array. */ int *ptr=&a[0]; /* NOT really ok, C will, again, let you. Same thing applys, though it won't complain at the same level, since characters are really only integers in C, it will sometimes convert without the complaint at a fairly high warning level. You are trying to assign the address of the first element in the character array.*/ scanf("%s", a); /*OK, well, at least it will work out all right. This is a case where a format specifier has the problem covered. Scanf "knows" that it is taking character data and storing it in a character array because it is using the special format S (for string). The 'a' designates enough for the compiler to resolve the storage issues, so it does it and fixes it, usually without complaing even though it technically wants an address of the beginning location to store characters in. */ int *ptr=&a; /* Big ERROR! You get it because the address operator refers to the name of an array without AN ELEMENT INDEX. Pointers can only store one address and it doesn't automatically assume it knows which one you'll want to start at in this case. So it throws a fit. */ (while ..) scanf("%s",&a); /* See the comment about the format specifier 's' above. Also realize that this is one of the cases where C will let you do it, but doing it could have unintended results. Let's say that the first loop through you enter 'Yes'. Then on the second pass you enter 'No'. Since C doesn't recognize strings without a '\n' character at the end, the contents of the array a will now be 'Nos\n' isntead of 'No\n' unless you first re-initialize the array with all '\n' characters. Not likely to be what some would expect or need in a program. */ Remember two things: 1) C compilers and C++ compilers both compile C code, but may have different criteria for some errors where variables are concerned. 2) Both language's compilers have different levels and types of preset warnings to allow for flexibility in some specific circumstances. Learn how to set them in your compiler(s) and set them wisely (as high as you can tolerate!) until you've learned the language well enough to judge what will actually harm a specific section of code.
  4. poteryaniy much

    On 2006-08-14 06:23:11


    from what i can tell, you are declaring a pointer to an address of a. what is a? is a initialized? is it an array? i think that is the problem here. a is null. provide some more details plz.