Solved

how can i store data in to a array

Posted on 2010-11-30
8
327 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 125 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 250 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
Technology Partners: 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 125 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 250 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Least Squares Curve Fitting 4 117
Microsoft C++ code failing in executable that worked 9 123
DCT of  2D array using fftw in c++ 9 97
How do i run a c++ file? 15 52
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 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