Meaning an array name s a pointer. BWAHAHAHAHAHA!!!

Nope. But in an expression an array name does "decays" into a pointer to the first element of the array.

how come it is declaring no where? BWAHAHAHAHAHA!!!

Because you did not initialize the pointers, nor did you later assign anywhere for them to point.

it is just an array of pointers to characters.

char *str[100];

Am I right? BWAHAHAHAHAHAHA!

Not where it counts.

do you know passing a multidimensional array to a function using a pointer?
you give me an example and you will see that that is not a luck!

meaning it is not a garbage.

In computer there is no such thing as LUCK! if the code is working. have you try what I post? Is it working or not? Are you the compiler of that code?

*(array) == array[0]
   
      *(array+1) == array[1]
   
      *(array+2) == array[2]
   
      *(array+n == array[n]

can you explain that why they are equal.

are you familiar with Pointers to Pionters?

** the two asterisk is pointers to pointers

BWAHAHAHAHAHAHA!

*sigh*

I'm not going to stop you from digging, but I'll try to help you out.

do you know passing a multidimensional array to a function using a pointer?
you give me an example and you will see that that is not a luck!

meaning it is not a garbage.

In computer there is no such thing as LUCK! if the code is working. have you try what I post? Is it working or not? Are you the compiler of that code?

*(array) == array[0]
   
      *(array+1) == array[1]
   
      *(array+2) == array[2]
   
      *(array+n == array[n]

can you explain that why they are equal.

are you familiar with Pointers to Pionters?

** the two asterisk is pointers to pointers

BWAHAHAHAHAHAHA!

Do you know that passing a multidimensional array is done differently from passing a pointer to a pointer?

Are you still stuck on the expression thing I already agreed you had right?

Would you like me to point you to specific FAQs? I recommend this whole thing:
http://c-faq.com/

[edit]I'll just start adding some...
http://c-faq.com/aryptr/pass2dary.html
http://c-faq.com/stdio/scanf1a.html
http://c-faq.com/aryptr/aryptrequiv.html


[edit=2]Say, I've got an idea. You're a master of C, so this should be easy. A string is just a bunch of characters than ends in a null character. So you ought to be able to print out a string character by character -- like this:

#include <stdio.h>

int main()
{
   char text[] = "hello world";
   int i;
   for ( i = 0; text[i]; ++i )
   {
      printf("text[%d] = '%c'\n", i, text[i]);
   }
   return 0;
}

/* my output
text[0] = 'h'
text[1] = 'e'
text[2] = 'l'
text[3] = 'l'
text[4] = 'o'
text[5] = ' '
text[6] = 'w'
text[7] = 'o'
text[8] = 'r'
text[9] = 'l'
text[10] = 'd'
*/

How might I do the same with your "fixed" code?

ok to cut it short THERE IS NO LUCK in Programming.
if the code works!
there is a hidden explanation on that.

Thank you David Sinkula nice to meet you. :-)

Next time don't say the code of others are garbage...

IF YOU CONSIDER THAT IT WAS ONLY A MATTER OF LUCK! THEN YOU DO NOT HAVE THE RIGHT TO BE IN THE COMPUTER WORLD BECAUSE ANY ANALYTICAL WORKS DOESN'T DEPEND ON LUCK!

thank you have a nice day.

commented: You're an idiot. -4
commented: You're clearly a troll, and dumber than a box of rocks as well. Luck counts for 0% in programming -3

ok to cut it short THERE IS NO LUCK in Programming.
if the code works!
there is a hidden explanation on that.

The definition of undefined behavior is that it can behave, by chance, as you expect it to. If you would like me to use the technical terminology, I can (oh, I already did that too).

BTW. The code you posted and are having a hissyfit about is undefined.

An array of pointers, like you have used, would need to point somewhere before you can write to it. For example,

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char *str1[5];
   char array[5][10];
   int i;
   /*
    * Dynamic allocation to assign pointers writable memory.
    */
   for ( i = 0; i < sizeof str1 / sizeof *str1; ++i )
   {
      str1[i] = malloc(10 * sizeof str1[i]);
      if ( str1[i] )
      {
         sprintf(str1[i], "string%d", i);
         puts(str1[i]);
         free(str1[i]);
      }
   }
   /*
    * Point the pointers to other writable memory.
    */
   for ( i = 0; i < sizeof str1 / sizeof *str1; ++i )
   {
      str1[i] = array[i];
      sprintf(str1[i], "string%d", i);
      puts(str1[i]);
   }
   return 0;
}

/* my output
string0
string1
string2
string3
string4
string0
string1
string2
string3
string4
*/

I've used sprintf instead of scanf to simulate the data input.

Points to be noted, raghuhr84

  • Give the complete program, that is, give #include<stdio.h> . Becomes easier for us to check.
  • Use proper indentation
  • Use code tags around your program (See the forum rules)

The problem is that you have not flushed stdin. Put fflush(stdin); before the scanf . That should do it.
Here is the complete program :-

#include <stdio.h>

int main()
{
	char str1[100];
	char str2[100];

	printf("Enter 1st string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str1);

	printf("Enter 2nd string\n");
	fflush(stdin);
	scanf("%[^\n]s",&str2);

	printf("1st string is %s\n", str1);
	printf("2nd string is %s\n", str2);

	return 0;
}

i dont know if you already solved this prob

you can just remove the "&" in the scanf statement
ex: scanf("%s, str1);

you can still use gets
ex: gets(str1);

sorry if methods like these are already mentioned

*SIGH*

this is a simple explanation on char *str[100];

To declare an array of pointers to characters.

char *str1[100];

char *letters[100];

char message = "the quick brown fox";
char *message = "the quick brown fox";

What is a pointer?
Pointer = is a numeric variable with a value that is the address of another variable.

Meaning every letter or character has a single pointer and single value on that statement.

BWAHAHAHAHAHAHA!!!

*SIGH*

this is a simple explanation on char *str[100];

To declare an array of pointers to characters.

char *str1[100];

char *letters[100];

char message = "the quick brown fox";
char *message = "the quick brown fox";

What is a pointer?
Pointer = is a numeric variable with a value that is the address of another variable.

Meaning every letter or character has a single pointer and single value on that statement.

BWAHAHAHAHAHAHA!!!

So scanf wants a pointer to characters, right? [edit]A single pointer to char for the terminally slow.[/edit] You give it a pointer to an array of characters, right? You understand that there is a difference, right?

Keep digging. But there is no need to accentuate your incompetence with "BWAHAHAHAHAHAHA!!!"

Here's another FAQ:
http://c-faq.com/aryptr/aryptr2.html

[edit]Oh, and since you bring it up and seem to have issues with this stuff, in your example, you can not modify that data pointed to by message. As in...

#include <stdio.h>

int main(void)
{
   char *message = "the quick brown fox";
   message[0] = 'T'; /* wrong */
   puts(message);
   return 0;
}

[edit=2]Having trouble with the character-by-character stuff? :P

commented: For feeding the trolls... -2

you can still use gets
ex: gets(str1);

sorry if methods like these are already mentioned

Yes. It's already been mentioned as a well known function to avoid like the plague.

[edit]Q: Why does everyone say not to use gets()?

Initializing and using an array of pointers to type char.

#include <stdio.h>

main()
{
   char *message[8] = { "a","b","c","d","e","f","g","h"};
   int count;
   for (count=0; count < 8; count++)
   printf("%s ", message[count]);
   printf("\n");
   return (0);
}

output: a b c d e f g h

So do you have a point or a question? I hope you understand the issues with scanf by now.

And message should really be const since the array members point to string literals and are nonmodifiable. Implicit int return value for main is rather out of date.

commented: Uphill all the way - that was a hell of a climb ;) +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.