[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how can i store data in to a array

Posted on 2010-11-30
8
Medium Priority
?
333 Views
Last Modified: 2012-05-10
hallo,
im trying to put data in to array its a name

but i dont know how

please help.
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam[50]);
getchar();
printf("well look %s",vnaam[50]);
getchar();    
}

Open in new window

0
Comment
Question by:kensy11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34242336
You don't need to specify the array index when writing to it:
#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[50];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 34242377
change the following statement:
scanf("%s",vnaam[50])
with
scanf("%s",vnaam)
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 total points
ID: 34242470
fyi - vnaam[50] in the scanf is simply the 51st entry into the vnaam array.
But since you define vnaam[50] to have only 50 entries, then you are actually referencing a char that is outside of the array. In some instances, your program will crash with a segment fault if you do this.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kensy11
ID: 34242774
thanks my code looks like this

i have another question what if i dont know how long the array will be, now its 50 but can just leave it blank ?



#include <stdio.h>
#include <conio.h>
int main (void)
{
 char vnaam[];
printf("Wat is u voornam ?");
scanf("%s",vnaam);
getchar();
printf("well look %s",vnaam);
getchar();    
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 34242949
>> char vnaam[];
Sorry, this is not valid. If you are filling up an array with scanf, you should make it much larger than any possible entry. After the entry, it will be null terminated.
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 34242964
I don't think you can. Normally you would size the char[] to the maximum size you were expecting.

#define MAX_STRING_SIZE 80

int main()
{
    char[MAX_STRING_SIZE] buffer;

    // scanf, etc, etc
}
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 1000 total points
ID: 34243010
To protect from overflow, you should consider using fgets which limits the number of chars transferred to the buffer. (In this case, from the console, you would use stdin as the file handle.)
   http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

But read this carefully w.r.t. newline char and null termination.
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 34250522
I'm not sure about scanf function but I remember that cin function in C++ takes 512 characters, not more.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question